Spring学习笔记 IOC|DI xml实现

Spring Framework 的特点Spring 框架具有以下几个特点。(spring官网)

  1. 方便解耦,简化开发Spring 就是一个大工厂,可以将所有对象的创建和依赖关系的维护交给 Spring 管理。
  2. 方便集成各种优秀框架Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如 Struts2、Hibernate、MyBatis 等)的直接支持。
  3. 降低 Java EE API 的使用难度Spring 对 Java EE 开发中非常难用的一些 API(JDBC、JavaMail、远程调用等)都提供了封装,使这些 API 应用的难度大大降低。
  4. 方便程序的测试Spring 支持 JUnit4,可以通过注解方便地测试 Spring 程序。
  5. AOP 编程的支持Spring 提供面向切面编程,可以方便地实现对程序进行权限拦截和运行监控等功能。
  6. 声明式事务的支持只需要通过配置就可以完成对事务的管理,而无须手动编程。

application.xml

applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">    
    
    
    
  
   
    
<bean id="us" class="com.itheima.service.impl.UserServiceImpl"/>   
    

    
    
<bean id="us2" class="com.itheima.factory.StaticFactory" factory-method="getInstance">bean>

    
    
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"/>   
    
<bean id="us3" factory-bean="instanceFactory" factory-method="getInstance"/>   

    
    
<bean id="us4" class="com.itheima.factory.UserServiceFactoryBean" />beans>

注入方式:setter注入和构造器注入

setter注入:
类中需要有对应属性的set方法
property标签:为对象的属性赋值属性:
name:属性名称
value:为属性设置简单类型数据–>设置不是IOC容器中管理的数据
ref:为属性设置引用类型数据 -->设置IOC容器中管理的对象

注意:
value属性和ref属性不能同时使用 两者只能同时使用一个
使用set注入比较灵活 可以注入多个 也可以一个都不注入

注入简单类型:基本数据类型+String

<bean id="us1" class="com.itheima.service.impl.UserServiceImpl01">    
<property name="username" value="黄帅"/>    
<property name="age" value="20"/>
bean>

注入引用类型:注入IOC容器中管理的对象数据到一个类中

<bean id="ud2" class="com.itheima.dao.impl.UserDaoImpl02"/>
<bean id="us2" class="com.itheima.service.impl.UserServiceImpl02">   
	<property name="userDao" ref="ud2"/>
bean>

构造器注入
类中需要有对应参数的构造方法constructor-arg标签:设置构造方法参数 可以有多个 写在bean标签中
属性:
name:构造方法参数名称
value:注入简单类型数据
ref:注入引用类型数据 注入IOC容器中管理的对象

带2个参数的构造函数 username address

 <bean id="us1" class="com.itheima.service.impl.UserServiceImpl03">   
 	<constructor-arg name="username" value="张三"/>   
 	<constructor-arg name="address" value="深圳"/>
 bean>

带3个参数的构造函数 username address userDao

<bean id="ud" class="com.itheima.dao.impl.UserDaoImpl02">bean>
<bean id="us2" class="com.itheima.service.impl.UserServiceImpl03">        
    <constructor-arg name="username" value="张三"/>        
    <constructor-arg name="address" value="深圳"/>        
    <constructor-arg name="userDao" ref="ud"/>
bean>

p命名空间注入:
基于setter注入,对setter注入简化,要求类中的属性必须提供set方法
使用步骤:
在applicationContext.xml中引入p命名空间xmlns:p=“http://www.springframework.org/schema/p”
在bean的开始标签中使用 p:属性名称=简单类型值 p:属性名称-ref=bean的Id


<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:p="http://www.springframework.org/schema/p"       
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="ud" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="us" class="com.itheima.service.impl.UserServiceImpl04" p:username="李四" p:address="惠州" p:userDao-ref="ud" />

DI自动装配注意:
自动装配用于引用类型依赖注入,不能对简单类型进行操作
使用按类型装配时(byType)必须保障容器中相同类型的bean唯一,推荐使用
使用按名称装配时(byName)必须保障容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用,(类中变量名和bean中id相同)
自动装配优先级低于setter注入与构造器注入,同时出现时自动装配配置失效

<bean id="ud" class="com.itheima.dao.impl.UserDaoImpl"/>

<bean id="us" class="com.itheima.service.impl.UserServiceImpl" autowire="byType">
等价于:<bean id="us" class="com.itheima.service.impl.UserServiceImpl" 
  			 name="userDao" ref="ud"/>
		bean>DI

注入集合类型数据
注意:
注入集合数据时,如果注入的是数组、List集合、Set集合 可以使用array、set、list三个标签中的任意一个都可以
如果注入的是Map集合或properties,两种集合的数据注入方式可以互换

<bean id="us6" class="com.itheima.service.impl.UserServiceImpl06">  
       
<property name="array">            
   	<array>                
   		<value>张三value>                
   		<value>李四value>                
   		<value>王五value>            
   	array>        
property>        

        
<property name="list">           
   	 <list>                
   		<value>张三1value>                
   		<value>李四1value>                
   		<value>王五1value>           
   	list>       
property>
       
       
<property name="set">            
   	<set>                
   		<value>张三2value>               
   		<value>李四2value>                
   		<value>王五2value>            
   	set>
property>
       
        
<property name="map">            
   	<map>               
   		<entry key="CN" value="中国">entry>               
   		<entry key="JP" value="小日本">entry>                
   		<entry key="USA" value="抽口感">entry>            
   	map>        
property>        

        
<property name="properties">            
   <props>                
   		<prop key="sz">深圳prop>                
   		<prop key="dg">东莞prop>                
   		<prop key="hz">惠州prop>            
   props>        			
property>    
bean>

引入外部properties文件

<context:property-placeholder location="classpath:db.properties"/>

使用:${配置文件中的key}获取key对应的数据

注意:
引入外部配置文件时,key不要设置为username(系统不区分大小写,例如userName也不行),因为username系统已存在该名称
解决方式:
1. 加一个下划线,user_name,
2. 在引入配置文件的地方设置local-override属性为"true" ,默认为false

<context:property-placeholder local-override ="true" location="classpath:db.properties"/>

import标签 引入子配置文件
引入resources文件夹中的spring-dao.xml文件,可以引用spring-dao.xml文件配置的交由IOC容器管理的类

<import resource="classpath:spring-dao.xml"/>

你可能感兴趣的:(Spring学习笔记 IOC|DI xml实现)