Spring中依赖注入的方式有两种:使用setter方法注入和使用构造器注入
第一种:set方法和构造器 -- 基本属性的注入
set注入是一种直接方式,缺点是它假设了所有的可变属性都可以通过set方法访问到。例如有些属性在创建时设置一次,以后不再改变。
首先创建一个类,类名为PersonServiceBean.java,代码为:
- package cn.csdn.hr.service;
- import java.util.Date;
- publicclass PersonServiceBean {
- //封装属性
- private String name;
- private String sex;
- private Integer age;
- private Date birth;
- public PersonServiceBean() {
- super();
- // TODO Auto-generated constructor stub
- }
- public PersonServiceBean(String name, String sex, Integer age, Date birth) {
- super();
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.birth = birth;
- }
- //setter方法来依赖注入
- publicvoid setName(String name) {
- this.name = name;
- }
- publicvoid setSex(String sex) {
- this.sex = sex;
- }
- publicvoid setAge(Integer age) {
- this.age = age;
- }
- publicvoid setBirth(Date birth) {
- this.birth = birth;
- }
- @Override
- public String toString() {
- return"PersonServiceBean [name=" + name + ", sex=" + sex + ", age="
- + age + ", birth=" + birth + "]";
- }
- }
package cn.csdn.hr.service;
import java.util.Date;
publicclass PersonServiceBean {
//封装属性
private String name;
private String sex;
private Integer age;
private Date birth;
public PersonServiceBean() {
super();
// TODO Auto-generated constructor stub
}
public PersonServiceBean(String name, String sex, Integer age, Date birth) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.birth = birth;
}
//setter方法来依赖注入
publicvoid setName(String name) {
this.name = name;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
publicvoid setAge(Integer age) {
this.age = age;
}
publicvoid setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return"PersonServiceBean [name=" + name + ", sex=" + sex + ", age="
+ age + ", birth=" + birth + "]";
}
}
在xml中用set方法注入,xml的名字为beanset.xml:
- <?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">
- <bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">
- <!-- 依赖注入的方式 -->
- <!--方式1 :通过set注入 -->
- <property name="name">
- <value>laowang</value>
- </property>
- <property name="sex">
- <value>女</value>
- </property>
- <property name="age">
- <value>23</value>
- </property>
- <!-- 出生日期 ref引用 -->
- <property name="birth" ref="date">
- <!-- 内部bean -->
- <!-- <bean class="java.util.Date"></bean> -->
- </property>
- </bean>
- <!-- 日期bean的声明 set注入 -->
- <bean id="date" class="java.util.Date">
- <property name="year">
- <value>2011</value>
- </property>
- <property name="month">
- <value>11</value>
- </property>
- <property name="date">
- <value>1</value>
- </property>
- </bean>
- </beans>
<?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">
<bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">
<!-- 依赖注入的方式 -->
<!--方式1 :通过set注入 -->
<property name="name">
<value>laowang</value>
</property>
<property name="sex">
<value>女</value>
</property>
<property name="age">
<value>23</value>
</property>
<!-- 出生日期 ref引用 -->
<property name="birth" ref="date">
<!-- 内部bean -->
<!-- <bean class="java.util.Date"></bean> -->
</property>
</bean>
<!-- 日期bean的声明 set注入 -->
<bean id="date" class="java.util.Date">
<property name="year">
<value>2011</value>
</property>
<property name="month">
<value>11</value>
</property>
<property name="date">
<value>1</value>
</property>
</bean>
</beans>
用构造器的方法注入:
- <?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">
- <!-- 构造器注入必须按顺序写,如果不按顺序写,按索引,从0开始,这样顺序可以换,属性必须全写,不全都写不对 -->
- <bean id="personServiceBean1" class="cn.csdn.hr.service.PersonServiceBean">
- <constructor-arg index="0">
- <value>老王</value>
- </constructor-arg>
- <constructor-arg>
- <value>女</value>
- </constructor-arg>
- <constructor-arg>
- <value>23</value>
- </constructor-arg>
- <constructor-arg>
- <!-- 内部bean,只在里面使用 -->
- <bean class="java.util.Date" />
- </constructor-arg>
- </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">
<!-- 构造器注入必须按顺序写,如果不按顺序写,按索引,从0开始,这样顺序可以换,属性必须全写,不全都写不对 -->
<bean id="personServiceBean1" class="cn.csdn.hr.service.PersonServiceBean">
<constructor-arg index="0">
<value>老王</value>
</constructor-arg>
<constructor-arg>
<value>女</value>
</constructor-arg>
<constructor-arg>
<value>23</value>
</constructor-arg>
<constructor-arg>
<!-- 内部bean,只在里面使用 -->
<bean class="java.util.Date" />
</constructor-arg>
</bean>
或者是:
- <bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">
- <constructor-arg type="java.lang.String" value="老王" />
- <constructor-arg type="java.lang.String" value="男" />
- <constructor-arg type="java.lang.Integer" value="23" />
- <constructor-arg type="java.util.Date" ref="date" />
- </bean>
- <bean name="date" class="java.util.Date"></bean>
- </beans>
- <STRONG><SPAN style="FONT-SIZE: 16px"> </SPAN></STRONG>
<bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean">
<constructor-arg type="java.lang.String" value="老王" />
<constructor-arg type="java.lang.String" value="男" />
<constructor-arg type="java.lang.Integer" value="23" />
<constructor-arg type="java.util.Date" ref="date" />
</bean>
<bean name="date" class="java.util.Date"></bean>
</beans>
测试的方法为:
- publicvoid test() {
- //第一步:获取应用程序上下文对象
- ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanset.xml");
- //第二步:根据应用程序上下文对象的gettBean
- PersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");
- System.out.println(personServiceBean.toString());
- }
- AN style="COLOR: black"> </SPAN>
publicvoid test() {
//第一步:获取应用程序上下文对象
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanset.xml");
//第二步:根据应用程序上下文对象的gettBean
PersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");
System.out.println(personServiceBean.toString());
}
注:setter方法的注入和构造方法的注入详细解说:
Setter方法的注入在xml中如果有没有注入的属性,则会默认为null,属性顺序的改变不会引起结果的改变。在这里,我们使用到了一个内部的bean,这个内部的bean只能供当前的属性使用,并且在setter中我们用的是ref,来引用已经创建好的内部bean。
构造方法的注入:在注入属性的时候属性的顺序在没有用index的情况下是不可以更改顺序的,更改会抛异常。如果向这样<constructor-argindex="0">,每一个标签后都加一个index,顺序的改变不会抛异常,而且index是从0开始的,从以上的代码中可以看出构造器的注入方式也可以指定类型
第二种:setter和构造器集合的注入
普通的注入
集合的注入的类名为TeacherServiceBean.java:
集合注入分为set、map、list和Properties
代码为:
- package cn.csdn.hr.service;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- public class TeacherServiceBean {
- // 集合的注入
- private List<String> list;
- private Set<String> set;
- private Map<String, String> map;
- private Properties properties;
- public TeacherServiceBean() {
- super();
- // TODO Auto-generated constructor stub
- }
- public TeacherServiceBean(List<String> list, Set<String> set,
- Map<String, String> map, Properties properties) {
- super();
- this.list = list;
- this.set = set;
- this.map = map;
- this.properties = properties;
- }
- public void setList(List<String> list) {
- this.list = list;
- }
- public void setSet(Set<String> set) {
- this.set = set;
- }
- public void setMap(Map<String, String> map) {
- this.map = map;
- }
- // 用户测试
- public List<String> getList() {
- return list;
- }
- public Set<String> getSet() {
- return set;
- }
- public Map<String, String> getMap() {
- return map;
- }
- public Properties getProperties() {
- return properties;
- }
- public void setProperties(Properties properties) {
- this.properties = properties;
- }
- @Override
- public String toString() {
- return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="
- + map + "]";
- }
- }
package cn.csdn.hr.service;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class TeacherServiceBean {
// 集合的注入
private List<String> list;
private Set<String> set;
private Map<String, String> map;
private Properties properties;
public TeacherServiceBean() {
super();
// TODO Auto-generated constructor stub
}
public TeacherServiceBean(List<String> list, Set<String> set,
Map<String, String> map, Properties properties) {
super();
this.list = list;
this.set = set;
this.map = map;
this.properties = properties;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
// 用户测试
public List<String> getList() {
return list;
}
public Set<String> getSet() {
return set;
}
public Map<String, String> getMap() {
return map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="
+ map + "]";
}
}
在xml中的配置,名字为bean2.xml
- <?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">
- <!-- 集合的注入 -->
- <bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">
- <property name="list">
- <list>
- <value>hp电脑1</value>
- <value>hp电脑2</value>
- <value>hp电脑3</value>
- <value>hp电脑4</value>
- <value>hp电脑5</value>
- <value>hp电脑6</value>
- </list>
- </property>
- <!-- set方式注入 -->
- <property name="set">
- <set>
- <value>计算机技术1</value>
- <value>计算机技术2</value>
- <value>计算机技术3</value>
- <value>计算机技术4</value>
- <value>计算机技术5</value>
- </set>
- </property>
- <!-- map集合的注入方式 -->
- <property name="map">
- <map>
- <entry>
- <key>
- <value>0001</value>
- </key>
- <value>java编程与开发1</value>
- </entry>
- <entry>
- <key>
- <value>0002</value>
- </key>
- <value>java编程与开发2</value>
- </entry>
- <entry>
- <key>
- <value>0003</value>
- </key>
- <value>java编程与开发3</value>
- </entry>
- </map>
- </property>
- <!-- prop集合的配置 -->
- <property name="properties">
- <props>
- <prop key="0x001">老王1</prop>
- <prop key="0x002">老王2</prop>
- <prop key="0x003">老王3</prop>
- </props>
- </property>
- </bean>
- </beans>
<?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">
<!-- 集合的注入 -->
<bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">
<property name="list">
<list>
<value>hp电脑1</value>
<value>hp电脑2</value>
<value>hp电脑3</value>
<value>hp电脑4</value>
<value>hp电脑5</value>
<value>hp电脑6</value>
</list>
</property>
<!-- set方式注入 -->
<property name="set">
<set>
<value>计算机技术1</value>
<value>计算机技术2</value>
<value>计算机技术3</value>
<value>计算机技术4</value>
<value>计算机技术5</value>
</set>
</property>
<!-- map集合的注入方式 -->
<property name="map">
<map>
<entry>
<key>
<value>0001</value>
</key>
<value>java编程与开发1</value>
</entry>
<entry>
<key>
<value>0002</value>
</key>
<value>java编程与开发2</value>
</entry>
<entry>
<key>
<value>0003</value>
</key>
<value>java编程与开发3</value>
</entry>
</map>
</property>
<!-- prop集合的配置 -->
<property name="properties">
<props>
<prop key="0x001">老王1</prop>
<prop key="0x002">老王2</prop>
<prop key="0x003">老王3</prop>
</props>
</property>
</bean>
</beans>
获取写为构造器的注入方式:
- <!-- 利用构造器的注入方式 -->
- <bean id="teacherServiceBean1" class="cn.csdn.hr.service.TeacherServiceBean">
- <constructor-arg>
- <list>
- <value>hp电脑1</value>
- <value>hp电脑2</value>
- <value>hp电脑3</value>
- <value>hp电脑4</value>
- <value>hp电脑5</value>
- <value>hp电脑6</value>
- </list>
- </constructor-arg>
- <constructor-arg>
- <set>
- <value>计算机技术1</value>
- <value>计算机技术2</value>
- <value>计算机技术3</value>
- <value>计算机技术4</value>
- <value>计算机技术5</value>
- </set>
- </constructor-arg>
- <constructor-arg>
- <map>
- <entry>
- <key>
- <value>0001</value>
- </key>
- <value>java编程与开发1</value>
- </entry>
- <entry>
- <key>
- <value>0002</value>
- </key>
- <value>java编程与开发2</value>
- </entry>
- <entry>
- <key>
- <value>0003</value>
- </key>
- <value>java编程与开发3</value>
- </entry>
- </map>
- </constructor-arg>
- <constructor-arg>
- <props>
- <prop key="0x001">老王1</prop>
- <prop key="0x002">老王2</prop>
- <prop key="0x003">老王3</prop>
- </props>
- </constructor-arg>
- </bean>
<!-- 利用构造器的注入方式 -->
<bean id="teacherServiceBean1" class="cn.csdn.hr.service.TeacherServiceBean">
<constructor-arg>
<list>
<value>hp电脑1</value>
<value>hp电脑2</value>
<value>hp电脑3</value>
<value>hp电脑4</value>
<value>hp电脑5</value>
<value>hp电脑6</value>
</list>
</constructor-arg>
<constructor-arg>
<set>
<value>计算机技术1</value>
<value>计算机技术2</value>
<value>计算机技术3</value>
<value>计算机技术4</value>
<value>计算机技术5</value>
</set>
</constructor-arg>
<constructor-arg>
<map>
<entry>
<key>
<value>0001</value>
</key>
<value>java编程与开发1</value>
</entry>
<entry>
<key>
<value>0002</value>
</key>
<value>java编程与开发2</value>
</entry>
<entry>
<key>
<value>0003</value>
</key>
<value>java编程与开发3</value>
</entry>
</map>
</constructor-arg>
<constructor-arg>
<props>
<prop key="0x001">老王1</prop>
<prop key="0x002">老王2</prop>
<prop key="0x003">老王3</prop>
</props>
</constructor-arg>
</bean>
测试的方法为:
- publicvoid teacharTest(){
- ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
- TeacherServiceBean teacherServiceBean = (TeacherServiceBean) ac.getBean("teacherServiceBean1");
- //list集合的注入方式
- List<String> list = teacherServiceBean.getList();
- for(String str:list){
- System.out.println(str.toString());
- }
- //set集合的注入方式
- Set<String> set = teacherServiceBean.getSet();
- for(String str:set){
- System.out.println(str.toString());
- }
- //map集合的注入方式
- Map<String, String> map = teacherServiceBean.getMap();
- Set<Entry<String,String>> mset = map.entrySet();
- for(Entry str:mset){
- System.out.println(str.toString());
- }
- //prop集合
- Properties properties = teacherServiceBean.getProperties();
- Set<Entry<Object, Object>> pset = properties.entrySet();
- for(Entry str:pset){
- System.out.println(str.toString());
- }
- }
publicvoid teacharTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
TeacherServiceBean teacherServiceBean = (TeacherServiceBean) ac.getBean("teacherServiceBean1");
//list集合的注入方式
List<String> list = teacherServiceBean.getList();
for(String str:list){
System.out.println(str.toString());
}
//set集合的注入方式
Set<String> set = teacherServiceBean.getSet();
for(String str:set){
System.out.println(str.toString());
}
//map集合的注入方式
Map<String, String> map = teacherServiceBean.getMap();
Set<Entry<String,String>> mset = map.entrySet();
for(Entry str:mset){
System.out.println(str.toString());
}
//prop集合
Properties properties = teacherServiceBean.getProperties();
Set<Entry<Object, Object>> pset = properties.entrySet();
for(Entry str:pset){
System.out.println(str.toString());
}
}
使用spring-util来完成集合的注入
在用util注入之前要先导入需要的xsd文件,
先引入http://www.springframework.org/schema/util
然后引入
http://www.springframework.org/schema/util/spring-util-2.5.xsd
最终的格式为:
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
</beans>
这样就可以使用util的工具了,使用的方法如下:
- <bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">
- <!-- list集合的注入 -->
- <property name="list">
- <util:list>
- <value>java学习语言</value>
- </util:list>
- </property>
- <!-- set集合的注入 -->
- <property name="set">
- <util:set>
- <value>set集合的注入</value>
- </util:set>
- </property>
- <!-- map集合的注入 -->
- <property name="map">
- <util:map>
- <entry key="0x0001" value="map集合的注入">
- </entry>
- </util:map>
- </property>
- <!-- properties的注入 -->
- <property name="properties">
- <util:properties>
- <prop key="0xxx">00java</prop>
- </util:properties>
- </property>
- </bean>
- <STRONG> </STRONG>
<bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean">
<!-- list集合的注入 -->
<property name="list">
<util:list>
<value>java学习语言</value>
</util:list>
</property>
<!-- set集合的注入 -->
<property name="set">
<util:set>
<value>set集合的注入</value>
</util:set>
</property>
<!-- map集合的注入 -->
<property name="map">
<util:map>
<entry key="0x0001" value="map集合的注入">
</entry>
</util:map>
</property>
<!-- properties的注入 -->
<property name="properties">
<util:properties>
<prop key="0xxx">00java</prop>
</util:properties>
</property>
</bean>
空值的注入,只要在属性里写:
- <property name="birth">
- <null/>
- </property>
<property name="birth">
<null/>
</property>
就可以了
注:我们可以看出,构造器的注入只需把set的注入的property标签改为<constructor-arg>标签就可以了