依赖注入的两种常用方式(构造器和Setter)与注入内容(装配数据)——Spring IOC/DI(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34598667/article/details/83308071

本章主要讲解一下Spring中依赖注入方式,接上一章依赖注入原理和方式:
https://blog.csdn.net/qq_34598667/article/details/83315669

依赖注入常用的两种方式以及注入的内容(装配数据)

Spring通过DI(依赖注入)实现IOC(控制反转),常用的注入方式主要有三种:构造方法注入,setter注入和接口注入。
本章重点讲解基本的两种方式构造器和setter,注解后面会单独一章讲解


案例准备

创建ApplicationContext.xml作为Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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 ... -->
</beans>

在新建一个包,com.oak.entity,新建实体类Personh和Man如下:

public class Person {
	private String name;
	private Integer age;
	//... 构造方法,getter,setter和toString方法略
}
public class Man {
	private Person person;
	//... 构造方法,getter,setter和toString方法略
}

新建一个com.oak.test,新建一个DITest类


构造器注入

容器调用带有一组参数的类构造方法完成依赖注入,使用的是〈bean〉标签中的〈constructor-arg〉元素
在xml中添加bean,使用构造器注入属性值,使用value属性进行参数值的注入

<?xml version="1.0" encoding="UTF-8"?>
<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="person" class="com.oak.entity.Person">
 		<constructor-arg value="二狗"/>
 		<constructor-arg value="18"/>
 	</bean>
</beans>

在DUTest类中添加测试方法test01并测试:

	@Test
	public void test01(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		Person person=ctx.getBean("person",Person.class);
		System.out.println(person);
	}

查看控制台输出:

Person [name=二狗, age=18]

1)多个参数时,把参数传递给构造函数可能会存在歧义

可以使用 index 属性来显式的指定构造函数参数的索引,索引从0开始
修改< bean >定义如下:

	<bean id="person" class="com.oak.entity.Person">
 		<constructor-arg index="0" value="二狗"/>
 		<constructor-arg index="1" value="18"/>
 	</bean>

2)使用type属性指定参数的类型

注意:基本类型有包装类型需要进行区分
修改< bean >定义如下:

	<bean id="person" class="com.oak.entity.Person">
 		<constructor-arg index="0" type="java.lang.String" value="二狗"/>
 		<constructor-arg index="1" type="java.lang.Integer" value="18"/>
 	</bean>

3)需要给参数指定一个引用(指向其他bean),使用ref属性

修改xml。添加一个bean定义

	<bean id="man" class="com.oak.entity.Man">
 		<constructor-arg ref="person"/>
 	</bean>

修改test01并测试:

	@Test
	public void test01(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		Person person=ctx.getBean("person",Person.class);
		System.out.println(person);
		Man man=ctx.getBean("man",Man.class);
		System.out.println(man);
	}

控制台输出结果:

Person [name=二狗, age=18]
Man [person=Person [name=二狗, age=18]]

setter方法注入

当容器调用一个无参的构造函数或一个无参的静态工厂方法来初始化bean,通过容器在bean 上调用setter设值函数 ,使用的是〈bean〉标签中的〈property〉元素
property的name属性指定类的属性名,需要一致,其他与构造器相同
在xml中添加一个新的bean

	<!-- setter方法注入 -->
 	<bean id="personSet" class="com.oak.entity.Person">
    	<property name="age" value="18"/>
    	<property name="name" value="二蛋"/>
   	</bean>

在测试类中新加一个test02方法

	@Test
	public void test02(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		Person person=ctx.getBean("personSet",Person.class);
		System.out.println(person);
	}

测试并查看控制台:

Person [name=二蛋, age=18]

注入的内容(装配数据)

1)参数值注入

上面已经讲过可以通过value进行参数值注入,使用ref进行bean对象的注入

2)注入集合

< list >:注入一列值,允许重复
< set >:注入一列值,不允许重复
< map >:注入键(名)值对的集合,名称和值可以是任何类型
< props >:注入键(名)值对的集合,名称和值可以是任何类
例:在com.oak.entity包中新建JavaCollection类如下:

public class JavaCollection {
	private List list;
	private Map map;
	private Properties prop;
	//构造方法,setter,getter和toString方法略
	}

在Spring配置中定义bean,修改xml,添加bean定义

   		<bean id="coll" class="com.oak.entity.JavaCollection">
   			<property name="list">
   				<!-- 注入集合 值可重复 set就不举例了-->
   				<list>
   					<value>list1</value>
   					<value>list1</value>
   				</list>
   			</property>
   			<property name="map">
   				<!-- 注入map -->
   				<map>
   					<entry key="二蛋">
   						<value>18</value>
   					</entry>
   				</map>
   			</property>
   			<property name="prop">
   				<!-- 注入properties -->
   				<props>
   					<prop key="二黑"></prop>
   				</props>
   			</property>
   		</bean>

在测试类中新加测试方法test03并测试:

	@Test
	public void test03(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		JavaCollection collection=ctx.getBean("coll",JavaCollection.class);
		System.out.println(collection);
	}

查看控制太输出:

JavaCollection [list=[list1, list1], map={二蛋=18}, prop={二黑=男}]

注入Spring表达式

Sprng引入一种跟EL表达式类型语法的Spring表达式,可以读取一个bean对象或者集合中的内容:
#{bean.属性}
在Spring配置中定义bean,修改xml,添加bean定义

	<!-- setter方法注入 -->
 	<bean id="personSet1" class="com.oak.entity.Person">
    	<property name="age" value="#{personSet.age}"/>
    	<property name="name" value="#{personSet.name}"/>
    </bean>

在测试类中添加test04并测试

	@Test
	public void test04(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
		Person person=ctx.getBean("personSet",Person.class);
		System.out.println(person);
		Person person1=ctx.getBean("personSet1",Person.class);
		System.out.println(person1);
	}

查看控制台结果

Person [name=二蛋, age=18]
Person [name=二蛋, age=18]

注入空字符串或者null

""和< null />
就不做案例了


下一章:无注解的自动装配以及方式
https://blog.csdn.net/qq_34598667/article/details/83317377

猜你喜欢

转载自blog.csdn.net/qq_34598667/article/details/83308071