依赖注入是Spring IoC容器实现反转控制的方式, Spring 的IoC容器以依赖注入的方式实现了Bean对象之间关系的维护。
两种方式的对象注入:
1、基于构造方法的依赖注入
先看一个简单的例子:
<1>实现Bean对象
package com.injection.bean;
public class ConstructInjectionBean {
private AnotherBean anotherBean;
private YetAnotherBean yetAnotherBean;
private int I;
public ConstructInjectionBean( AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.anotherBean = anotherBean;
this.yetAnotherBean = yetAnotherBean;
this.i = i;
}
public void display() {
System.out.println( "ConstructInjectionBean:" );
System.out.println( this.yetAnotherBean );
System.out.println( i );
}
}
<2>配置bean
<?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-2.0.xsd">
<bean id="anotherBean" class="com.injection.bean.AnotherBean"/>
<bean id="yetAnotherBean" class="com.injection.bean.YetAnotherBean"/>
<!-- 基于构造方法的依赖注入 -->
<bean id="constructInjectionBean" class="com.injection.bean.ConstructInjectionBean">
<!-- 使用ref标签来指定被注入的参数 -->
<constructor-arg>
<ref bean="anotherBean"/>
</constructor-arg>
<constructor-arg ref="yetAnotherBean"/>
<!-- 直接定义常量来作为被注入的参数 -->
<constructor-arg type="int" value="1"/>
</bean>
</beans>
测试主程序
public class InjectionMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ConstructInjectionBean constructInjectionBean = (ConstructInjectionBean)context.getBean( "constructInjectionBean" );
constructInjectionBean.display();
}
}
2、基于setter方法的依赖注入
使用setter方法的依赖注入,需要首先通过调用无参构造器或无参静态工厂方法实例化Bean,然后再调用该Bean的setter方法实现setter依赖注入;
<1>实现bean对象
public class Person {
private Long pid;
private String pname;
private Student student;
private List lists;
private Set sets;
private Map maps;
private Properties properties;
public Person(String pname,Student student){
this.pname = pname;
this.student = student;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
<2>配置bean
<?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-2.5.xsd">
<!--
把person和student纳入spring容器
-->
<bean id="person" class="cn.itcast.spring01.di.Person">
<!--
property代表Person类的一个属性
name为属性的名称
value代表属性的值
String 类型和基本类型是用value来进行赋值的
-->
<property name="pid" value="2"></property>
<property name="pname" value="里活命"></property>
<!-- 给引用赋值 -->
<property name="student">
<!--
利用ref给对象赋值
-->
<ref bean="student"/>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="maps">
<map>
<entry key="entry1">
<value>
aaa
</value>
</entry>
<entry key="entry2">
<value>
bbb
</value>
</entry>
<entry key="entry3">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">
prop1
</prop>
<prop key="prop2">
prop2
</prop>
</props>
</property>
<!--
定义person中的构造器
用<constructor-arg></constructor-arg>来确定唯一的构造器
index
参数的索引值
ref
给引用类型赋值
value
给基本类型赋值
-->
<constructor-arg value="aaaa"></constructor-arg>
<constructor-arg ref="student"></constructor-arg>
</bean>
<bean id="student" class="cn.itcast.spring01.di.Student"></bean>
</beans>
<3>测试主程序
public class PersonTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring01/di/applicationContext.xml");
Person person = (Person)context.getBean("person");
System.out.println(person.getPid());
System.out.println(person.getPname());
person.getStudent().say();
List list = person.getLists();
System.out.println("size:"+list.size());
}
}
<特殊>bean中的构造方法参数有多个时
public Person(String pname,String ss,Student student){
this.pname = pname;
this.ss = ss;
this.student = student;
}
<?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-2.5.xsd">
<!--
把person和student纳入spring容器中
-->
<bean id="person" class="cn.itcast.spring0401.di.constructor.Person">
<!--
定义person中的构造器
用<constructor-arg></constructor-arg>来确定唯一的构造器
index
参数的索引值
ref
给引用类型赋值
value
给基本类型赋值
-->
<constructor-arg index="0" value="aaaa"></constructor-arg>
<constructor-arg index="1" value="sss"></constructor-arg>
<constructor-arg index="2" ref="student"></constructor-arg>
</bean>
<bean id="student" class="cn.itcast.spring0401.di.constructor.Student"></bean>
</beans>