Spring依赖注入

依赖注入的概念

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

1)构造函数注入(constructor-arg标签)

通过配置的方式,给构造函数的成员变量赋值。

代码:

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() {
        
    }
}

配置如下:

<bean id="customerService" class="com.service.impl.CustomServiceImpl">
	<constructor-arg name="name" value="张三"></constructor-arg>
	<constructor-arg name="age" value="18"></constructor-arg>
	<constructor-arg name="birthday" value="now"></constructor-arg>
</bean>

<bean id="now" class="java.util.Date"></bean>

配置解析:
index:指定参数在构造函数参数列表的索引位置
type: 指定参数在构造函数中的数据类型
name: 指定参数在构造函数的名称
value:它能赋的值是其它的bean类型,也就是说,必须得是在配置文件中配置过的bean

2)set方法注入(property标签)

代码:

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() {

    }
}

配置如下:

<bean id="customerService" class="com.service.impl.CustomServiceImpl">
	<property name="name" value="张三"></constructor-arg>
	<property name="age" value="18"></property>
	<property name="birthday" value="now"></property>
</bean>

<bean id="now" class="java.util.Date"></bean>

配置解析:
name: 找的是类中set方法后面的部分
value:给属性赋值是基本数据类型和String类型的
ref:给属性复制是其它的bean类型

3)使用p名称空间注入数据(p标签)

这种方式是通过在xml中导入p空间名称,使用p:propertyName 来注入数据,它的本质仍然是调用类中的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() {

    }
}

配置如下:

06Spring - Spring依赖注入(基于XML)

4)注入集合属性

代码:

package cn.itcast.domain;

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

public class CollBean 
{
	public CollBean()
	{
		System.out.println(11111);
	}
	
	private String[] ss;
	private List ll;
	private Map mm;
	private Properties properties; //键值对
	
	public void setSs(String[] ss) {
		this.ss = ss;
	}
	public void setLl(List ll) {
		this.ll = ll;
	}
	public void setMm(Map mm) {
		this.mm = mm;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}




	@Override
	public String toString() {
		return "CollBean [ss=" + Arrays.toString(ss) + ", ll=" + ll + ", mm=" + mm + ", properties=" + properties + "]";
	}
	
	
	
	
}

配置文件:

 <bean id="collBean" class="cn.itcast.domain.CollBean">
	   	 	 <property name="ss">
	   	 	 		<!-- 数组类型 -->
	   	 	 		<list>
	   	 	 			<value>aaa</value>
	   	 	 			<value>bbb</value>
	   	 	 			<value>ccc</value>
	   	 	 		</list>
	   	 	 </property>
	   	 	 
	   	 	 <property name="ll">
	   	 	 		<!-- list类型  -->
	   	 	 		<list>
	   	 	 			<value>111</value>
	   	 	 			<value>222</value>
	   	 	 			<ref bean="car"/>
	   	 	 		</list>
	   	 	 </property>
	   	 	 
	   	 	 <property name="mm">
	   	 	 	<!-- map -->
	   	 	 	<map>
	   	 	 		<entry key="k1" value="aaa"></entry>
	   	 	 		<entry key="k2" value="bbbb"></entry>
	   	 	 		<entry key="k3" value-ref="car"></entry>
	   	 	 	</map>
	   	 	 </property>
	   	 	 
	   	 	 <property name="properties">
	   	 	 	<!-- properties类型 -->
	   	 	 	<props>
	   	 	 		<prop key="hibernate.username">root</prop>
	   	 	 		<prop key="hibernate.password">1234</prop>
	   	 	 	</props>
	   	 	 </property>
	   	 </bean>

分类:

技术点:

相关文章: