第四章 Spring的依赖注入

本文深入解析了依赖注入的概念及其在Spring框架中的应用,包括构造方法注入、set方法注入、p名称空间注入以及集合属性的注入方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖注入的概念

构造方法注入
  • 就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让spring框架来为我们注入。
public class CustomerServiceImpl implements ICustomerService {
	
	private String name;
	private Integer age;
	private Date birthday;
		
	public CustomerServiceImpl(String name, Integer age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}

	@Override
	public void saveCustomer() {
		System.out.println(name+","+age+","+birthday);	
	}
}
<!-- 使用构造函数的方式,给service中的属性传值
	要求:
		类中需要提供一个对应参数列表的构造函数。
	涉及的标签:
		constructor-arg
			属性:
				index:指定参数在构造函数参数列表的索引位置
				type:指定参数在构造函数中的数据类型
				name:指定参数在构造函数中的名称		
				
				=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
				
				value:它能赋的值是基本数据类型和String类型
				ref:它能赋的值是其他bean类型,也就是说,必须得是在配置文件中配置过的bean
	 -->
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
	<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 CustomerServiceImpl implements ICustomerService {
	
	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 saveCustomer() {
		System.out.println(name+","+age+","+birthday);	
	}
}
<!-- 通过配置文件给bean中的属性传值:使用set方法的方式
	涉及的标签:
		property
		属性:
			name:找的是类中set方法后面的部分
			ref:给属性赋值是其他bean类型的
			value:给属性赋值是基本数据类型和string类型的
	实际开发中,此种方式用的较多。
-->
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
		<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 CustomerServiceImpl4 implements ICustomerService {
	
	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 saveCustomer() {
		System.out.println(name+","+age+","+birthday);	
	}
}
<bean id="customerService" 
		  class="com.itheima.service.impl.CustomerServiceImpl4"
		  p:name="test" p:age="21" p:birthday-ref="now"/>

注入集合属性

  • 就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。我们这里介绍注入数组,List,Set,Map,Properties。
public class CustomerServiceImpl implements ICustomerService {
	
	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 saveCustomer() {
		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="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
	<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
	<!-- 给数组注入数据 -->
	<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值