Spring中屬性注入詳解。本站提示廣大學習愛好者:(Spring中屬性注入詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Spring中屬性注入詳解正文
本文演示了int、String、數組、list、set、map、Date等屬性的注入。
個中Date類型的注入則是借助了Spring供給的屬性編纂器來完成的,起首是用到的五個實體類
package com.jadyer.model;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 罕見屬性的注入
* @see 包含int,String,Array,list,set,map,Date的注入
*/
public class Bean11 {
private Integer intValue;
private String strValue;
private String[] arrayValue;
private List listValue;
private Set setValue;
private Map mapValue;
private Date dateValue;
/* 七個屬性的setter和getter略 */
}
package com.jadyer.model;
public class Bean22 {
private Bean33 bean33;
private Bean44 bean4422; //注入:與屬性名有關,與setBean44()有關
private Bean55 bean55;
/* 三個屬性的setter和getter略 */
}
package com.jadyer.model;
public class Bean33 {
private Integer id;
private String name;
private String sex;
/* 三個屬性的setter和getter略 */
}
package com.jadyer.model;
public class Bean44 {
private Integer id;
private String name;
private String sex;
private Integer age;
/* 四個屬性的setter和getter略 */
}
package com.jadyer.model;
public class Bean55 {
private String password;
/* 關於password的setter和getter略 */
}
然後是我們自界說的java.util.Date類型轉換器
package com.jadyer.util;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* java.util.Date屬性編纂器。相當於類型轉換器。這裡是將String轉為Date型
* @see ----------------------------------------------------------------------------------------
* @see 該示例重要讓年夜家曉得Spring也有這類機制,不是讓年夜家今後寫屬性編纂器
* @see 須要寫屬性編纂器的概率太少了,只需曉得Spring也有相似的機制便可以了
* @see ----------------------------------------------------------------------------------------
* @see 所謂的屬性編纂器,就是將Spring設置裝備擺設文件中的字符串轉換成響應的Java對象
* @see Spring內置了一些屬性編纂器,也能夠自界說屬性編纂器
* @see 自界說屬性編纂器事,須繼續PropertyEditorSupport類並覆寫setAsText()辦法
* @see 最初再將自界說的屬性編纂器注入到Spring中,便可
* @see ----------------------------------------------------------------------------------------
*/
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String pattern; //將轉換的格局放到設置裝備擺設文件中,讓Spring注入出去
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println("======UtilDatePropertyEditor.setAsText()======" + text);
try {
Date date = new SimpleDateFormat(pattern).parse(text);
this.setValue(date); //留意:這裡放出來的是一個java.util.Date對象,故輸入的時光是默許的格局
} catch (ParseException e) {
e.printStackTrace();
throw new IllegalArgumentException(text); //持續向上拋參數不法的異常
}
}
}
用到的針對一切實體類的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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-lazy-init="true">
<!-- default-lazy-init="true"屬性的解釋,詳見InjectionTest.java類的第49行 -->
<!-- default-autowire="byName或byType",這是Spri0ng供給的主動拆卸bean的兩種方法byName和byType,詳解略 -->
<!-- ***********************【LAZY====延遲初始化】*********************************************************************** -->
<!-- 履行testInjection22()時默許的會輸入======UtilDatePropertyEditor.setAsText()======2010年06月04日 -->
<!-- 即此時並未設置default-lazy-init="true",這解釋Bean11中的dateValue屬性的值被注入了 -->
<!-- 現實上,默許的Spring在創立ApplicationContext時,會將設置裝備擺設文件中一切的對象實例化並停止注入 -->
<!-- 如許做的利益是假如Spring設置裝備擺設文件中的某些設置裝備擺設寫錯了,它連忙就可以檢測出來 -->
<!-- 而Struts1.X的設置裝備擺設文件,假如某個類寫錯了,是不會出成績的,只要在真正履行的時刻,才會出成績 -->
<!-- 關於Spring而言,也能夠采取相干的屬性延遲設置裝備擺設文件的初始化,即default-lazy-init="true" -->
<!-- 即只要真正應用的時刻,再去New這個對象,再為屬性注入。這時候就觸及到LAZY,即延遲初始化 -->
<!-- 只需修正Spring設置裝備擺設文件便可,如<beans xsi:schemaLocation="http://www...." default-lazy-init="true"> -->
<!-- 這時候的感化規模即全部設置裝備擺設文件,同理也可對各個<bean>標簽的lazy-init屬性停止零丁設置裝備擺設 -->
<!-- 但普通都不會這麼設置,而是在BeanFactory創立的時刻,即完成注入,如許也便於檢討失足誤 -->
<!-- ***************************************************************************************************************** -->
<bean id="bean11" class="com.jadyer.model.Bean11">
<property name="intValue" value="123"/><!-- 注入時,字符串123會主動轉換為int型 -->
<property name="strValue" value="Hello_Spring"/>
<property name="arrayValue">
<list>
<value>array11</value>
<value>array22</value>
</list>
</property>
<property name="listValue">
<list>
<value>list11</value>
<value>list22</value>
</list>
</property>
<property name="setValue">
<set>
<value>set11</value>
<value>set22</value>
</set>
</property>
<property name="mapValue">
<map>
<entry key="key11" value="value11"/>
<entry key="key22" value="value22"/>
</map>
</property>
<property name="dateValue" value="2010年06月04日"/><!-- 這裡Date格局應與applicationContext-editor.xml設置裝備擺設的雷同 -->
</bean>
<bean id="bean22" class="com.jadyer.model.Bean22">
<property name="bean33" ref="bean33"/>
<property name="bean44" ref="bean44"/>
<property name="bean55" ref="bean55"/>
</bean>
<bean id="bean55" class="com.jadyer.model.Bean55">
<property name="password" value="123"/>
</bean>
</beans>
用到的針對公共實體類的applicationContext-common.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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 應用籠統bean提掏出公共設置裝備擺設 -->
<!-- 起首指定<bean>標簽的abstract屬性為true,然後在其它<bean>中指定其parent便可 -->
<bean id="AbstractBean" abstract="true">
<property name="id" value="2"/>
<property name="name" value="張起靈"/>
<property name="sex" value="男"/>
</bean>
<bean id="bean33" class="com.jadyer.model.Bean33" parent="AbstractBean"/>
<bean id="bean44" class="com.jadyer.model.Bean44" parent="AbstractBean">
<property name="age" value="26"/>
</bean>
</beans>
<!-- 應用AbstractBean之前的bean33和bean44的本相以下 -->
<!--
<bean id="bean33" class="com.jadyer.model.Bean33">
<property name="id" value="100"/>
<property name="name" value="張三"/>
<property name="sex" value="男"/>
</bean>
<bean id="bean44" class="com.jadyer.model.Bean44">
<property name="id" value="100"/>
<property name="name" value="張三"/>
<property name="sex" value="男"/>
<property name="age" value="90"/>
</bean>
-->
用到的針對java.util.Date屬性編纂器的applicationContext-editor.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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="utilDatePropertyEditor" class="com.jadyer.util.UtilDatePropertyEditor">
<property name="pattern" value="yyyy年MM月dd日"/>
</bean>
<!-- 檢查源碼得知,在CustomEditorConfigurer類的131行供給了一個setCustomEditors辦法,所以就可以夠注入了 -->
<bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value-ref="utilDatePropertyEditor"/>
</map>
</property>
</bean>
</beans>
<!-- 也能夠應用外部<bean>把utilDatePropertyEditor寫在外部 -->
<!-- 如許就只要它本身有權應用了,內部就沒法應用了 -->
<!-- 因為不供給外界拜訪,所之內部<bean>沒有id屬性 -->
<!-- 示例以下 -->
<!--
<bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="com.jadyer.util.UtilDatePropertyEditor">
<property name="pattern" value="yyyy年MM月dd日"/>
</bean>
</entry>
</map>
</property>
</bean>
-->
最初是應用JUnit3.8寫的單位測試類
package com.jadyer.junit;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jadyer.model.Bean11;
import com.jadyer.model.Bean22;
public class PropertyInjectionTest extends TestCase {
private ApplicationContext factory;
@Override
protected void setUp() throws Exception {
/****====讀取單一的設置裝備擺設文件====****/
//factory = new ClassPathXmlApplicationContext("applicationContext.xml");
/****====應用數組讀取多個設置裝備擺設文件====****/
//如許就會把兩個設置裝備擺設文件作為一個來應用,外面上看是作為兩個應用的
//其實外部是作為一個應用的,所以在多個設置裝備擺設文件中,外面的id不克不及反復
//然則name屬性可以反復,就似乎人的身份證編號和名字的差別是一樣的
//String[] configLocations = new String[]{"applicationContext.xml", "applicationContext-editor.xml"};
//factory = new ClassPathXmlApplicationContext(configLocations);
/****=====應用 * 婚配形式讀取多個設置裝備擺設文件====****/
//業界風行的一句話:商定優於設置裝備擺設
//所以說當有了一個同一的比擬好的商定以後,便可以應用框架供給的功效,削減設置裝備擺設量
//別的:假如沒有讀取到applicationContext-*.xml,此時即使存在applicationContext.xml,它也不會讀的
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
}
/**
* 該辦法演示的是罕見屬性的注入,包含int,String,Array,list,set,map,Date的注入
* @see 個中Date類型的注入則是借助了Spring屬性編纂器來完成的
*/
public void testInjection11() {
//Bean11 bean11 = new Bean11(); //若簡略的new,那末它的屬性是不會被注入的。注入的條件必需是從IoC容器中拿出來的,才會注入
Bean11 bean11 = (Bean11)factory.getBean("bean11"); //此時bean11就是從IoC容器中獲得到的,所以它的依附就會被全體注入
System.out.println("bean11.intValue=" + bean11.getIntValue());
System.out.println("bean11.strValue=" + bean11.getStrValue());
System.out.println("bean11.arrayValue=" + bean11.getArrayValue());
System.out.println("bean11.listValue=" + bean11.getListValue());
System.out.println("bean11.setValue=" + bean11.getSetValue());
System.out.println("bean11.mapValue=" + bean11.getMapValue());
System.out.println("bean11.dateValue=" + bean11.getDateValue());
}
/**
* 該辦法重要演示的是將公共的設置裝備擺設停止籠統,以削減設置裝備擺設量
*/
public void testInjection22() {
Bean22 bean22 = (Bean22)factory.getBean("bean22");
System.out.println("bean22.bean33.id=" + bean22.getBean33().getId());
System.out.println("bean22.bean33.name=" + bean22.getBean33().getName());
System.out.println("bean22.bean33.sex=" + bean22.getBean33().getSex());
System.out.println("bean22.bean44.id=" + bean22.getBean44().getId());
System.out.println("bean22.bean44.name=" + bean22.getBean44().getName());
System.out.println("bean22.bean44.sex=" + bean22.getBean44().getSex());
System.out.println("bean22.bean44.age=" + bean22.getBean44().getAge());
System.out.println("bean22.bean55.password=" + bean22.getBean55().getPassword());
}
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。