Spring学习3 - <Bean>的属性、DI依赖注入三种方式

13 篇文章 0 订阅

1. 属性设置 - XML配置文件、以及与其等价的注解形式

注意: - 所有Bean都懒加载,不过优先级比较低

容器启动时
属性
lazy-init:true/false(default) - 懒加载
scope
singleton:单例
prototype:每次获取都会被创建新对象
request:每次请求都会被创建新对象
session:不同session获取会被创建新对象
init-method:创建对象时,被触发方法
destroy-method:对象被回收时,被触发的方法
Bean注解
@PostConstructor:等价于init-method属性
@PreDestory:等价于destory-method属性
@Scope('singleton'):等价于scope属性
@Compone:将Bean注册到Spring容器上

2. 依赖注入DI的方式

即类一旦被初始化、类内的某些属性通过Spring容器的XML文件一同自动的初始化

2.1 属性自动注入 - 不推荐

通过Spring自带的Junit测试工具进行测试DI注入

autowire属性值
default/no:不会自动注入
byType:根据setxx方法的参数类型进行自动注入
byName:根据setxx方法的方法名去掉set并且将首字母小写方法名 - 与 一致
constructor:根据构造器的参数类型进行注入
代码准备阶段

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)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BwhY7MBu-1576777014905)(en-resource://database/13850:1)]

代码测试
@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)]

2.2 子节点构造器注入 - 不推荐
的属性
index:构造器参数的位置,从0开始
name:构造器形参的变量名
type:根据构造器的形参类型
ref:被注入的bean的name属性名
value:可直接写字面变量的值

  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)]


  测试代码:

@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)]

2.3 子节点手动注入
2.3.1 Property不是多值类
属性
name:调用setxxx() 去掉set并且将首字母小写
ref:bean的name或者id名


  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)]


  由运行结果可知 - 运行成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-igHMt9Lo-1576777014934)(en-resource://database/13872:1)]

2.3.1 是多值类 - Property需要子、孙节点

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E6b6FOKE-1576777014937)(en-resource://database/13874:1)]

  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)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值