文章目录
1. 属性设置 - XML配置文件、以及与其等价的注解形式
注意: - 所有Bean都懒加载,不过优先级比较低
2. 依赖注入DI的方式
即类一旦被初始化、类内的某些属性通过Spring容器的XML文件一同自动的初始化
2.1 属性自动注入 - 不推荐
通过Spring自带的Junit测试工具进行测试DI注入
代码准备阶段
EmpService3 - 通过自动注入empDao3自动的会被初始化-无须手动new
public class EmpService3 {
EmpDao3 empDao3;
EmpService3() {
System.out.println("EmpService3被实例化");
}
EmpService3(EmpDao3 empDao) {
System.out.println("EmpService3被实例化");
}
public void setEmpDao3(EmpDao3 empDao3) {
this.empDao3 = empDao3;
}
}
EmpDao3
public class EmpDao3 {
EmpDao3() {
System.out.println("EmpDao3被实例化");
}
}
Spring容器配置文件 - 注意共有6个类,只是上面两个的复制品,读者自行复制,改一下编号就可以
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean name="empDao" class="top.linruchang.dao.EmpDao" ></bean>
<bean name="empService" class="top.linruchang.service.EmpService" autowire="constructor"></bean>
<bean name="empDao2" class="top.linruchang.dao.EmpDao2" ></bean>
<bean name="empService2" class="top.linruchang.service.EmpService2" autowire="byType"></bean>
<bean name="empDao3" class="top.linruchang.dao.EmpDao3" ></bean>
<bean name="empService3" class="top.linruchang.service.EmpService3" autowire="byName"></bean>
</beans>
代码测试
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestSpringContainer {
// 该注解的作用 - 自动初始化EmpService - 如果能自动注入,则对象实例化时,属性也会相应得到实例化(依赖注入)
@Autowired
EmpService es;
@Autowired
EmpService2 es2;
@Autowired
EmpService3 es3;
@Test
public void test1() {}
}
运行结果
2.2 子节点构造器注入 - 不推荐
Spring容器的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
>
<!-- 构造器参数类型 - type是被注入的Class全限定名 ref被注入的bean-name属性名 -->
<bean name="empDao" class="top.linruchang.dao.EmpDao" ></bean>
<bean name="empService" class="top.linruchang.service.EmpService">
<constructor-arg type="top.linruchang.dao.EmpDao" ref="empDao"></constructor-arg>
</bean>
<!-- 构造器参数类型 - name构造器形参的变量名 ref被注入的bean-name属性名 -->
<bean name="empDao2" class="top.linruchang.dao.EmpDao2" ></bean>
<bean name="empService2" class="top.linruchang.service.EmpService2">
<constructor-arg name="empDao233" ref="empDao2"></constructor-arg>
</bean>
<!-- 构造器参数类型 - index构造器第几个形参 ref被注入的bean-name属性名 -->
<bean name="empDao3" class="top.linruchang.dao.EmpDao3" ></bean>
<bean name="empService3" class="top.linruchang.service.EmpService3">
<constructor-arg index="0" ref="empDao3"></constructor-arg>
</bean>
</beans>
测试代码:
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestSpringContainer {
@Autowired
EmpService es;
@Autowired
EmpService2 es2;
@Autowired
EmpService3 es3;
@Test
public void test1() {
}
}
运行效果
2.3 子节点手动注入
2.3.1 Property不是多值类
Spring容器配置文件
<bean name="empDao" class="top.linruchang.dao.EmpDao"></bean>
<bean name="empService" class="top.linruchang.service.EmpService">
<property name="empDao1" ref="empDao"></property>
</bean>
<bean name="empDao2" class="top.linruchang.dao.EmpDao2"></bean>
<bean name="empService2" class="top.linruchang.service.EmpService2">
<property name="empDao2" ref="empDao2"></property>
</bean>
<bean name="empDao3" class="top.linruchang.dao.EmpDao3"></bean>
<bean name="empService3" class="top.linruchang.service.EmpService3">
<property name="empDao3" ref="empDao3"></property>
</bean>
由运行结果可知 - 运行成功
2.3.1 是多值类 - Property需要子、孙节点
Emp代码
public class Emp {
List<String> hobbies1;
Set<String> hobbies2;
Map<Integer, String> hobbies3;
Properties hobbies4;
String[] hobbies5;
public void setHobbies1(List<String> hobbies1) {
this.hobbies1 = hobbies1;
System.out.println("hobbies1" + hobbies1);
}
public void setHobbies2(Set<String> hobbies2) {
this.hobbies2 = hobbies2;
System.out.println("hobbies2" + hobbies2);
}
public void setHobbies3(Map<Integer, String> hobbies3) {
this.hobbies3 = hobbies3;
System.out.println("hobbies3" + hobbies3);
}
public void setHobbies4(Properties hobbies4) {
this.hobbies4 = hobbies4;
System.out.println("hobbies4" + hobbies4);
}
public void setHobbies5(String[] hobbies5) {
this.hobbies5 = hobbies5;
System.out.println("hobbies5" + Arrays.toString(hobbies5));
}
}
配置文件 - 注入的属性是List、Set、Map、数组、Properties( 其实也是Map )
<bean name="emp" class="top.linruchang.domain.Emp">
<property name="hobbies1">
<list>
<value>篮球</value>
<value>足球</value>
</list>
</property>
<property name="hobbies2">
<set>
<value>篮球</value>
<value>足球</value>
</set>
</property>
<property name="hobbies3">
<map>
<entry key="1" value="篮球"></entry>
<entry key="2" value="足球"></entry>
</map>
</property>
<property name="hobbies4">
<props>
<prop key="1">篮球</prop>
<prop key="2">足球</prop>
</props>
</property>
<property name="hobbies5">
<array>
<value>篮球</value>
<value>足球</value>
</array>
</property>
</bean>
Spring单元测试代码
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class DITest {
@Autowired
Emp emp;
@Test
public void test() {
System.out.println(emp);
}
}
运行结果