spring的依赖注入

spring的依赖注入

1、依赖注入的概念

它是spring框架核心ioc的具体实现方式。简单的说,就是坐等框架把对象传入,而不用我们自己去获取。

2、构造函数注入

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

  • java类
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>

3、set方法注入

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

  • java类
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>

4、使用p名称空间注入数据

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

  • java类
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);	
	}
}


  • 配置文件
<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="customerService" 
		  class="com.itheima.service.impl.CustomerServiceImpl4"
		  p:name="test" p:age="21" p:birthday-ref="now"/>
</beans>

5、集合属性的注入

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

  • java类
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>
记得快乐
原文地址:https://www.cnblogs.com/Y-wee/p/13814481.html