主要內(nèi)容:
- 普通屬性注入
- 自定義屬性編輯器
- 公共屬性注入
一、普通屬性注入
實(shí)體類:
Bean1.java
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
private Date dateValue;
相關(guān)getter和setter方法
Bean2.java
private Bean3 bean3;
private Bean4 bean4;
private Bean5 bean5;
Bean3.java
private int id;
private String name;
private String password;
Bean4.java
private int id;
private String name;
Bean5.java
private int age;
配置:applicationContext-beans.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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="bean1" class="com.bjsxt.spring.Bean1">
<property name="strValue" value="Hello" />
<!-- <property name="intValue" value="123"/> -->
<property name="intValue">
<value>123</value>
</property>
<property name="listValue">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<property name="setValue">
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
<property name="arrayValue">
<list>
<value>array1</value>
<value>array2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="k1" value="v1" />
<entry key="k2" value="v2" />
</map>
</property>
<property name="dateValue">
<value>2008-08-15</value>
</property>
</bean>
</beans>
說明:
- 1.spring允許有多個(gè)配置文件,在使用的時(shí)候可以這樣:
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
可以看到這樣使用時(shí)配置文件名的前面部分需要相同笛洛。 - 2.在配置時(shí)如果使用value屬性則記得加上雙引號禁熏,如果使用value標(biāo)簽則不需要屈嗤。
測試:
package com.bjsxt.spring;
import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectionTest extends TestCase {
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
}
public void testInjection1() {
Bean1 bean1 = (Bean1)factory.getBean("bean1");
System.out.println("bean1.strValue=" + bean1.getStrValue());
System.out.println("bean1.intValue=" + bean1.getIntValue());
System.out.println("bean1.listValue=" + bean1.getListValue());
System.out.println("bean1.setValue=" + bean1.getSetValue());
System.out.println("bean1.arrayValue=" + bean1.getArrayValue());
System.out.println("bean1.mapValue=" + bean1.getMapValue());
System.out.println("bean1.dateValue=" + bean1.getDateValue());
}
public void testInjection2() {
Bean2 bean2 = (Bean2)factory.getBean("bean2");
System.out.println("bean2.bean3.id=" + bean2.getBean3().getId());
System.out.println("bean2.bean3.name=" + bean2.getBean3().getName());
System.out.println("bean2.bean3.password=" + bean2.getBean3().getPassword());
System.out.println("bean2.bean4.id=" + bean2.getBean4().getId());
System.out.println("bean2.bean4.name=" + bean2.getBean4().getName());
System.out.println("bean2.bean5.age=" + bean2.getBean5().getAge());
}
}
說明:
- 1.首先注意繼承TestCase類然后覆寫setUp方法偶宫。
- 2.對于Bean1類前面所有屬性這樣配置都是沒有問題的侈贷,也就是說對于一些基本數(shù)據(jù)類型谐檀,spring是可以自動轉(zhuǎn)換的抡谐,但是如果是日期類型,則配置像:
<property name="dateValue">
<value>2008-08-15</value>
</property>
```是不能自動轉(zhuǎn)換的稚补,所以這里我們需要像struts2中一樣童叠,自己寫一個(gè)屬性編輯器烫沙。
#二帚桩、自定義屬性編輯器
* 自定義屬性編輯器,spring配置文件中的字符串轉(zhuǎn)換成相應(yīng)的對象進(jìn)行注入,spring已經(jīng)有內(nèi)置的屬性編輯器男图,我們可以根據(jù)需求自己定義屬性編輯器。
* 如何定義屬性編輯器结耀?
* 繼承```PropertyEditorSupport```類抑党,覆寫```setAsText()```方法,參見:```UtilDatePropertyEditor.java```
* 將屬性編輯器注冊到spring中撬碟,參見:```applicationContext-editor.xml```
* 注意:配置的時(shí)候```CustomEditorConfigurer```的屬性名不要寫錯诞挨,不能隨意寫,名字是```customEditors```呢蛤。
* 注意:一定不要將日期格式屬性的settter方法忘記惶傻,這是注入的關(guān)鍵。
**```UtilDatePropertyEditor.java```**
package com.bjsxt.spring;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
-
java.util.Date屬性編輯器
*/
public class UtilDatePropertyEditor extends PropertyEditorSupport {private String format="yyyy-MM-dd";
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println("UtilDatePropertyEditor.saveAsText() -- text=" + text);SimpleDateFormat sdf = new SimpleDateFormat(format); try { Date d = sdf.parse(text); this.setValue(d); } catch (ParseException e) { e.printStackTrace(); }
}
public void setFormat(String format) {
this.format = format;
}
}
**配置:```applicationContext-editor.xml.java```**
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.bjsxt.spring.UtilDatePropertyEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</entry>
</map>
</property>
</bean>
<!--
<bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertyEditor"></bean>
-->
</beans>
**說明:**
* 可以看到對于日期的轉(zhuǎn)換方法和以前我們平時(shí)使用的轉(zhuǎn)換方法是一樣的其障。這里需要注意的是轉(zhuǎn)換完之后需要使用方法setValue將轉(zhuǎn)換后的值返回回去银室。
* 配置的時(shí)候可以使用外部bean,然后使用entry標(biāo)簽的value屬性給配置進(jìn)去励翼,但是我們也可以使用內(nèi)部bean蜈敢,內(nèi)部bean就只能被其所在bean使用了,內(nèi)部bean不需要id汽抚。
* 對于日期格式我們可以在類中寫死抓狭,當(dāng)然我們也可以將日期格式讓spring幫我們注入進(jìn)去,前提是我們提供setter方法造烁,此時(shí)在自定義編輯器類中日期格式就不需要給值了否过。
#三、公共屬性的注入
如果有一個(gè)類```Bean2.java```膨蛮,其包含三個(gè)屬性叠纹,這三個(gè)屬性分別是```Bean3.java、Beean4.java敞葛、Bean5.java```誉察。
**```Bean2.java```**
private Bean3 bean3;
private Bean4 bean4;
private Bean5 bean5;
**```Bean3.java```**
private int id;
private String name;
private String password;
**```Bean4.java```**
private int id;
private String name;
**```Bean5.java```**
private int age;
如果按照一般方式進(jìn)行配置:
**```applicationContext-beans.xml```**
<bean id="bean2" class="com.bjsxt.spring.Bean2">
<property name="bean3" ref="bean3" />
<property name="bean4">
<ref bean="bean4" />
</property>
<property name="bean5" ref="bean5" />
</bean>
<bean id="bean3" class="com.bjsxt.spring.Bean3">
<property name="id" value="1000" />
<property name="name">
<value>Jack</value>
</property>
<property name="password" value="123" />
</bean>
<bean id="bean4" class="com.bjsxt.spring.Bean4">
<property name="id" value="1000" />
<property name="name" value="Jack" />
</bean>
<bean id="bean5" class="com.bjsxt.spring.Bean5">
<property name="age" value="20" />
</bean>
**說明:**這里我們可以發(fā)現(xiàn)```Bean3.java、Bean4.java```的屬性有公共部分惹谐,這里我們只是距離持偏,所以公共部分不是很多,那當(dāng)兩個(gè)類的公共部分很多時(shí)如果我們都按照普通方式那樣配置顯然很多余氨肌,這里我們需要配置一些公共屬性鸿秆。
**```applicationContext-other.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="beanAbstract" abstract="true">
<property name="id" value="1000"/>
<property name="name" value="Jack"/>
</bean>
<bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
<property name="name" value="Tom"/>
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>
**說明:**可以看到我們將公共屬性抽取出來,當(dāng)然對于公共屬性bean我們需要使用```abstract="true"```指明怎囚。而如果一個(gè)類要使用公共屬性也需要使用```parent="beanAbstract"```指明卿叽,如果和公共屬性中配置的值是一樣的桥胞,那么就不需要再次配置,如果值有變化則需要重復(fù)配置其value考婴,同時(shí)對于多出公共屬性的屬性還是需要額外配置的贩虾。
**最后:**這里只是簡單介紹了一下容器注入,更詳細(xì)的使用請參考文檔沥阱。