Spring总结——依赖注入(DI)


依赖注入(DI)

依赖注入简介

1、什么是依赖注入

  • ​ DI—Dependency Injection,即“依赖注入”是控制反转的一种实现方式。
  • 由ioc容器在运行期间,动态的将某种依赖关系注入到对象之中也就是获取对象的过程由管理编程有ioc容器主动注入,通过反射机制实现

2、使用依赖注入的目的

  • 谁依赖于谁:当然是应用程序依赖于IoC容器;
  • 为什么需要依赖:应用程序需要IoC容器来提供对象需要的外部资源;
  • 谁注入谁:很明显是IoC容器注入应用程序某个对象,应用程序依赖的对象;
  • 注入了什么:就是注入某个对象所需要的外部资源(包括对象、资源、常量数据)

依赖注入的方式

1、构造器注入

  • 使用的标签:constructor-arg
  • 属性标签中的属性
    type:用于指定构造函数中的某个或者某些参数的数据类型
    index:用于指定构造函数的位置,从0开始
    name:用于指定构造函数中的参数的名字,更为常用
    value:为基本数据类型和String类型注入具体的内容

xml文件

<?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="p" class="com.jp.domain.Person">
		<constructor-arg index="0" type="java.lang.String" value="张三"></constructor-arg>
		<constructor-arg index="1" type="java.lang.Integer" value="18"></constructor-arg>
	</bean>
</beans>

代码

public class Demo {
	/*
	 * 方式一:
	 * 通过构造器传入值
	 */
	@Test
	public void testConstructor(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p = (Person) ac.getBean("p");
		System.out.println(p.name);
		System.out.println(p.age);
	}
}

2、Setter方法注入

  • 使用的标签:property
  • 不可以提供构造函数,因为构造函数注入和set方法注入是不兼容的
  • 标签中的属性:
    ​ ① name:用于注入时所调用的方法名
    ​ ② value:为基本数据类型和String类型注入具体的内容
    ​ ③ ref:用于指定其他的bean类型,它指定的是spring容器中出现过的bean对象

xml文件

<?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="p" class="com.jp.domain.Person">
		<property name="name" value="张三"></property>
		<property name="age" value="18"></property>
		<property name="s" ref="student"></property>
	</bean>
	
	<bean id="student" class="com.jp.domain.Student"></bean>
	
</beans>

代码

public class Demo {
	/*
	 * 方式二
	 * 通过Set方法传入值
	 */
	@Test
	public void testSetter(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p = (Person) ac.getBean("p");
		System.out.println(p.getName());
		System.out.println(p.getAge());
		System.out.println(p.getS());
	}
}

3、集合属性的注入

List属性注入

  • 用于给List集合注入的标签:listarrayset

Set属性注入

  • 用于给Set集合注入的标签:set

Map属性注入

  • 用于给map集合注入的标签:mapentry

Properties属性注入

  • 用于给Properties集合注入的标签:propsprop

xml文件

<?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="mc" class="com.jp.di.MyCollection">
		<property name="list">
			<list>
				<value>123</value>
				<value>456</value>
				<ref bean="student"/>
				<ref bean="p"/>
			</list>
		</property>
		
		<property name="set">
			<set>
				<value>aaa</value>
				<value>bbb</value>
				<ref bean="student"></ref>
				<ref bean="p"></ref>
			</set>
		</property>
		
		<property name="map">
			<map>
				<entry key="aa" value="11"></entry>
				<entry key="bb" value-ref="p"></entry>
				<entry key-ref="p" value-ref="student"></entry>
			</map>
		</property>
		
		<property name="prop">
			<props>
				<prop key="name">张三</prop>
				<prop key="age">18</prop>
			</props>
		</property>
	</bean>
</beans>

对象类代码
MyCollection类

public class MyCollection {
	private List list;
	private Set set;
	private Map map;
	private Properties prop;
	
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Set getSet() {
		return set;
	}
	public void setSet(Set set) {
		this.set = set;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	
}

Person类

public class Person {
	private String name;
	private Integer age;
	private Student s;
	
	public Person(){
		System.out.println("Person对象已创建...");
	}
	
	public Person(String name, Integer age){
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public void talk(){
		System.out.println("讲话...");
	}
	
	public Student getS() {
		return s;
	}

	public void setS(Student s) {
		this.s = s;
	}
}

Student类

public class Student {
	private String name;
	private int age;
	
	public Student() {
		
	}

	public Student(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}	
}

测试代码

public class Demo {
	/*
	 * 方式三:
	 * 通过集合传入值
	 * 1、List集合
	 * 
	 * 2、Set集合
	 * 
	 * 3、Map集合
	 * 
	 * 4、Properties集合
	 */
	@Test
	public void testList(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		MyCollection mc = (MyCollection) ac.getBean("mc");
		System.out.println(mc.getList());
		System.out.println(mc.getSet());
		System.out.println(mc.getMap());
		System.out.println(mc.getProp());
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值