文章目录
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>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qlpEgpTO-1576777014901)(en-resource://database/13848:1)]](https://i-blog.csdnimg.cn/blog_migrate/01d207cc2e9b9c71ccb82b6704dbf4bf.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BwhY7MBu-1576777014905)(en-resource://database/13850:1)]](https://i-blog.csdnimg.cn/blog_migrate/c84d4f89676d502ab60023f086254a4a.png)
代码测试
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestSpringContainer {
// 该注解的作用 - 自动初始化EmpService - 如果能自动注入,则对象实例化时,属性也会相应得到实例化(依赖注入)
@Autowired
EmpService es;
@Autowired
EmpService2 es2;
@Autowired
EmpService3 es3;
@Test
public void test1() {}
}
运行结果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1YNs2NNz-1576777014920)(en-resource://database/13852:1)]](https://i-blog.csdnimg.cn/blog_migrate/ea2a074830099b303c6d95ecd3c37c0a.png)
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>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g0vFxPKp-1576777014924)(en-resource://database/13858:1)]](https://i-blog.csdnimg.cn/blog_migrate/dfc073f1880d1a55f9715a61c0f6eb28.png)
测试代码:
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestSpringContainer {
@Autowired
EmpService es;
@Autowired
EmpService2 es2;
@Autowired
EmpService3 es3;
@Test
public void test1() {
}
}
运行效果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rxOR2Abj-1576777014929)(en-resource://database/13860:1)]](https://i-blog.csdnimg.cn/blog_migrate/129905837237f4ce2d473b3232ba87b6.png)
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>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cLQFWby6-1576777014932)(en-resource://database/13868:1)]](https://i-blog.csdnimg.cn/blog_migrate/c6a865cab528cbed664f8a6f6689e4aa.png)
由运行结果可知 - 运行成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-igHMt9Lo-1576777014934)(en-resource://database/13872:1)]](https://i-blog.csdnimg.cn/blog_migrate/e55eff4e6ab1b2411dc50efd9a26d879.png)
2.3.1 是多值类 - Property需要子、孙节点
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E6b6FOKE-1576777014937)(en-resource://database/13874:1)]](https://i-blog.csdnimg.cn/blog_migrate/bda050c067babf0055349aadf24ffd05.png)
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);
}
}
运行结果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-urECQUSu-1576777014941)(en-resource://database/13876:1)]](https://i-blog.csdnimg.cn/blog_migrate/7e2453944c870e2ae0de2ea87d768831.png)

本文深入解析Spring框架中依赖注入(DI)的多种方式,包括属性自动注入、子节点构造器注入及子节点手动注入,同时介绍了Bean的生命周期管理与属性设置方法。
1006

被折叠的 条评论
为什么被折叠?



