spring 的依赖注入

依赖注入(Dependency Injection)是Spring框架的核心,通过控制反转,将对象的创建交给Spring管理。本文详细介绍了构造函数注入和set方法注入,以及如何通过P名称空间注入数据。此外,还展示了如何注入集合属性,包括数组、List、Set、Map和Properties。
摘要由CSDN通过智能技术生成

依赖注入的概念

依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。
我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。
ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取

构造函数注入

就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。具体代码如下:

public class AccountServiceImpl implements IAccountService {

	private String name;
	private Integer age;
	private Date birthday;
	
	public AccountServiceImpl(String name, Integer age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}
	
	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
	}
}
<!-- 使用构造函数的方式,给 service 中的属性传值
	要求:
		类中需要提供一个对应参数列表的构造函数。
	涉及的标签:
		constructor-arg
		属性:
			index:指定参数在构造函数参数列表的索引位置
			type:指定参数在构造函数中的数据类型
			name:指定参数在构造函数中的名称 用这个找给谁赋值
		=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
	value:它能赋的值是基本数据类型和 String 类型
	ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
	
	-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">

<constructor-arg name="name" value="徐毅"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>

set 方法注入

就是在类中提供需要注入成员的 set 方法。具体代码如下:

public class AccountServiceImpl implements IAccountService {

	private String name;
	private Integer age;
	private Date birthday;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
	}
}
<!-- 通过配置文件给 bean 中的属性传值:使用 set 方法的方式
	涉及的标签:
		property``属性:
		name:找的是类中 set 方法后面的部分
		ref:给属性赋值是其他 bean 类型的
		value:给属性赋值是基本数据类型和 string 类型的
	实际开发中,此种方式用的较多。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="name" value="test"></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>`

使用 p 名称空间注入数据(本质还是调用 set 方法)

此种方式是通过在 xml 中导入 p 名称空间,使用 p:propertyName 来注入数据,它的本质仍然是调用类中的set 方法实现注入功能。

/**
* 使用 p 名称空间注入,本质还是调用类中的 set 方法
*/
public class AccountServiceImpl4 implements IAccountService {

	private String name;
	private Integer age;
	private Date birthday;
	
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);
	}
}

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 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="accountService"
 class="com.itheima.service.impl.AccountServiceImpl4"
 p:name="test" p:age="21" p:birthday-ref="now"/>
</beans>

注入集合属性

顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。
我们这里介绍注入数组,List,Set,Map,Properties。具体代码如下:

public class AccountServiceImpl implements IAccountService {

	private String[] myStrs;
	private List<String> myList;
	private Set<String> mySet;
	private Map<String,String> myMap;
	private Properties myProps;
	
	public void setMyStrs(String[] myStrs) {
		this.myStrs = myStrs;
	}
	
	public void setMyList(List<String> myList) {
		this.myList = myList;
	}
	
	public void setMySet(Set<String> mySet) {
		this.mySet = mySet;
	}
	
	public void setMyMap(Map<String, String> myMap) {
		this.myMap = myMap;
	}
	
	public void setMyProps(Properties myProps) {
		this.myProps = myProps;
	}
	
	@Override
	public void saveAccount() {
		System.out.println(Arrays.toString(myStrs));
		System.out.println(myList);
		System.out.println(mySet);
		System.out.println(myMap);
		System.out.println(myProps);
	}
}

<!-- 注入集合数据
	List 结构的:
		array,list,set
	Map 结构的
		map,entry,props,prop
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
	<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
	<!-- 给数组注入数据 -->
	<property name="myStrs">
		<set>
			<value>AAA</value>
			<value>BBB</value>
			<value>CCC</value>
		</set>
	</property>
	<!-- 注入 list 集合数据 -->
	<property name="myList">
		<array>
			<value>AAA</value>
			<value>BBB</value>
			<value>CCC</value>
			</array>
	</property>
	<!-- 注入 set 集合数据 -->
	<property name="mySet">
		<list>
			<value>AAA</value>
			<value>BBB</value>
			<value>CCC</value>
		</list>
	</property>
	<!-- 注入 Map 数据 -->
	<property name="myMap">
		<props>
			<prop key="testA">aaa</prop>
			<prop key="testB">bbb</prop>
		</props>
	</property>
	<!-- 注入 properties 数据 -->
	<property name="myProps">
		<map>
			<entry key="testA" value="aaa"></entry>
			<entry key="testB">
			<value>bbb</value>
			</entry>
		</map>
	</property>
</bean>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值