Spring依赖注入 DI

DI:Dependency Injection(依赖注入)
从字面上分析:
IoC:指将对象的创建权,反转给了Spring容器;
DI :指Spring创建对象的过程中,将对象依赖属性(简单值,集合,对象)通过配置设值给该对象。
IoC和DI其实是同一个概念的不同角度描述,DI相对IoC而言,明确描述了“被注入对象依赖IoC容器配置依赖对象”。

所谓的依赖注入,就是属性不创建对象,通过配置文件的配置将Spring容器里面的对象注入给对应的属性

依赖注入有三种方式
1.setter注入(属性注入)
2.构造器注入
3.p命名空间注入

1、setter注入(属性注入)

setter注入,(也可以称之为属性注入)
使用setter注入:
1,使用bean元素的property子元素设置;
 a,简单类型值,直接使用value赋值;
 b,引用类型,使用ref赋值;
 c,集合类型,直接使用对应的集合类型元素即可。
2,spring通过属性的setter方法注入值;
3,在配置文件中配置的值都是string,spring可以自动的完成类型的转换

a、简单值和非集合普通对象注入

pojo

--Department
package com.spring.pojo;

public class Department {
	private Integer id;
	private String name;

	public Department() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Department(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + "]";
	}

}

–Employee

package com.spring.pojo;

public class Employee {
	private Integer age;
	private String name;
	private Department dept;

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(Integer age, String name, Department dept) {
		super();
		this.age = age;
		this.name = name;
		this.dept = dept;
	}

	public Integer getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

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

	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	@Override
	public String toString() {
		return "Employee [age=" + age + ", name=" + name + ", dept=" + dept + "]";
	}
}

applicationContext.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="myDept" class="com.spring.pojo.Department">
		<!-- setter方法注入: 属性注入 
			<property name="" value="">
			name : 属性名称
			value : 基本数据类型+String类型的值注入
			ref : 引用类型(对象类型)的注入
			value 和ref 只能二选一
		-->
		<property name="id" value="1001"/>
		<property name="name" value="研发部"/>
	</bean>
	
	<bean id="emp" class="com.spring.pojo.Employee">
		<property name="age" value="12"/>
		<property name="name" value="张三"/>
		<property name="dept" ref="myDept"/>
	</bean>

</beans>

测试代码

	@Test
	public void testName() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Department dept = applicationContext.getBean("myDept", Department.class);
		Employee employee = applicationContext.getBean("emp",Employee.class);
		System.out.println(dept);
		System.out.println(employee);
	}

结果图

在这里插入图片描述

b、集合类型注入

pojo

package com.spring.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionBean {
	private Set<String> set;
	private List<String> list;
	private String[] array;
	private Map<String, String> map;
	private Properties prop; // 读取本地 xxx.properties文件(本质就是一个Map集合)

	public Set<String> getSet() {
		return set;
	}

	public void setSet(Set<String> set) {
		this.set = set;
	}

	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public String[] getArray() {
		return array;
	}

	public void setArray(String[] array) {
		this.array = array;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public Properties getProp() {
		return prop;
	}

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	@Override
	public String toString() {
		return "CollectionBean [set=" + set + ", list=" + list + ", \narray=" + Arrays.toString(array) + ", map=" + map
				+ ", prop=" + prop + "]";
	}

}

applicationContext.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="collection" class="com.spring.pojo.CollectionBean">
		<!-- set集合 -->
		<property name="set">
			<set>
				<value>set1</value>
				<value>set1</value>
				<value>set2</value>
			</set>
		</property>
		<!-- list集合 -->
		<property name="list">
			<list>
				<value>list1</value>
				<value>list2</value>
				<value>list3</value>
			</list>
		</property>
		<!-- 数组array -->
		<property name="array">
			<array>
				<value>array1</value>
				<value>array2</value>
				<value>array3</value>
			</array>
		</property>
		<!-- 集合map -->
		<property name="map">
			<map>
				<entry key="name" value="zhangsan"/>
				<entry key="age" value="123"/>
			</map>
		</property>
		<!-- properties对象 -->
		<property name="prop">
			<props>
				<prop key="jdbc.username">root</prop>
				<prop key="jdbc.password">123456</prop>
			</props>
		</property>
	</bean>
</beans>

测试代码

@Test
	public void testName() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		//getBean(clazz);该方法必须保证spring容器中只有一个CollectionBean类型的对象
		CollectionBean bean = applicationContext.getBean(CollectionBean.class);
		System.out.println(bean);
	}

测试结果

在这里插入图片描述

2、构造器类型

pojo

跟上面Department和Employee一致

applicationContext.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="my_dept" class="com.spring.pojo.Department">
		<!-- 
		   1.index:在构造器中的参数索引(0开始)
		   2.type:在构造器中的参数的类型
		   3.name:在构造器中按照构造器的参数名字设置值 
           4.type:参数类型,默认自动匹配
           5.value:值类型注入(String,各种数据类型)
           6.ref:引用类型注入
	 -->
		<constructor-arg index="0" name="id" type="java.lang.Integer" value="1002"/>
		<constructor-arg index="1" name="name" type="java.lang.String" value="策划部"/>
	</bean>

	<bean id="emp" class="com.spring.pojo.Employee">
		<constructor-arg index="0" name="age" type="java.lang.Integer" value="23"/>
		<constructor-arg index="1" name="name" type="java.lang.String" value="王五"/>
		<constructor-arg index="2" name="dept" type="com.spring.pojo.Department" ref="my_dept"/>
	</bean>
</beans>

测试代码

	@Test
	public void testName() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Department dept = applicationContext.getBean("my_dept", Department.class);
		Employee employee = applicationContext.getBean("emp",Employee.class);
		System.out.println(dept);
		System.out.println(employee);
	}

测试结果

在这里插入图片描述

3、p命名空间注入

pojo

跟上面Department和Employee一致

applicationContext.xml

使用p命名空间注入先在约束上面引入 p标签xmlns:p="http://www.springframework.org/schema/p"
底层还是使用set方法注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="my_dept" class="com.spring.pojo.Department"
		p:id="1003"
		p:name="销售部"
	/>
	<!--
		使用p命名空间规则:当属性为String或基本数据类型,则使用p:属性名=值
					      当属性为对象时,则使用p:属性名-ref=对象bean的id
	  -->
	<bean id="emp" class="com.spring.pojo.Employee"
		p:age="30"
		p:name="武则天"
		p:dept-ref="my_dept"
	/>
	
</beans>

测试代码

@Test
	public void testName() throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Department dept = applicationContext.getBean("my_dept", Department.class);
		Employee employee = applicationContext.getBean("emp",Employee.class);
		System.out.println(dept);
		System.out.println(employee);
	}

测试结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值