最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中屬性注入詳解

 更新時間:2016年10月19日 16:46:02   作者:玄玉  
這篇文章主要為大家詳細(xì)介紹了Spring中屬性注入,演示了int、String、數(shù)組、list等屬性的注入,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文演示了int、String、數(shù)組、list、set、map、Date等屬性的注入。
其中Date類型的注入則是借助了Spring提供的屬性編輯器來實現(xiàn)的,首先是用到的五個實體類

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; //注入:與屬性名無關(guān),與setBean44()有關(guān) 
  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; 
  /* 關(guān)于password的setter和getter略 */ 
} 

然后是我們自定義的java.util.Date類型轉(zhuǎn)換器

package com.jadyer.util; 
 
import java.beans.PropertyEditorSupport; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
/** 
 * java.util.Date屬性編輯器。相當(dāng)于類型轉(zhuǎn)換器。這里是將String轉(zhuǎn)為Date型 
 * @see ---------------------------------------------------------------------------------------- 
 * @see 該示例主要讓大家知道Spring也有這種機制,不是讓大家以后寫屬性編輯器 
 * @see 需要寫屬性編輯器的幾率太少了,只要知道Spring也有類似的機制就可以了 
 * @see ---------------------------------------------------------------------------------------- 
 * @see 所謂的屬性編輯器,就是將Spring配置文件中的字符串轉(zhuǎn)換成相應(yīng)的Java對象 
 * @see Spring內(nèi)置了一些屬性編輯器,也可以自定義屬性編輯器 
 * @see 自定義屬性編輯器事,須繼承PropertyEditorSupport類并覆寫setAsText()方法 
 * @see 最后再將自定義的屬性編輯器注入到Spring中,即可 
 * @see ---------------------------------------------------------------------------------------- 
 */ 
public class UtilDatePropertyEditor extends PropertyEditorSupport { 
  private String pattern; //將轉(zhuǎn)換的格式放到配置文件中,讓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對象,故輸出的時間是默認(rèn)的格式 
    } catch (ParseException e) { 
      e.printStackTrace(); 
      throw new IllegalArgumentException(text); //繼續(xù)向上拋參數(shù)非法的異常 
    } 
  } 
} 

用到的針對所有實體類的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====延遲初始化】*********************************************************************** --> 
  <!-- 執(zhí)行testInjection22()時默認(rèn)的會輸出======UtilDatePropertyEditor.setAsText()======2010年06月04日 --> 
  <!-- 即此時并未設(shè)置default-lazy-init="true",這說明Bean11中的dateValue屬性的值被注入了 --> 
  <!-- 事實上,默認(rèn)的Spring在創(chuàng)建ApplicationContext時,會將配置文件中所有的對象實例化并進行注入 --> 
  <!-- 這樣做的好處是如果Spring配置文件中的某些配置寫錯了,它立刻就能檢測出來 --> 
  <!-- 而Struts1.X的配置文件,如果某個類寫錯了,是不會出問題的,只有在真正執(zhí)行的時候,才會出問題 --> 
  <!-- 對于Spring而言,也可以采用相關(guān)的屬性延遲配置文件的初始化,即default-lazy-init="true" --> 
  <!-- 即只有真正使用的時候,再去New這個對象,再為屬性注入。這時就涉及到LAZY,即延遲初始化 --> 
  <!-- 只需修改Spring配置文件即可,如<beans xsi:schemaLocation="http://www...." default-lazy-init="true"> --> 
  <!-- 這時的作用范圍即整個配置文件,同理也可對各個<bean>標(biāo)簽的lazy-init屬性進行單獨配置 --> 
  <!-- 但一般都不會這么設(shè)置,而是在BeanFactory創(chuàng)建的時候,即完成注入,這樣也便于檢查出錯誤 --> 
  <!-- ***************************************************************************************************************** --> 
   
  <bean id="bean11" class="com.jadyer.model.Bean11"> 
    <property name="intValue" value="123"/><!-- 注入時,字符串123會自動轉(zhuǎn)換為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格式應(yīng)與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>標(biāo)簽的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> 
 
<!-- 也可以使用內(nèi)部<bean>把utilDatePropertyEditor寫在內(nèi)部 --> 
<!-- 這樣就只有它自己有權(quán)使用了,外部就無法使用了 --> 
<!-- 由于不提供外界訪問,所以內(nèi)部<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"); 
     
    /****====利用數(shù)組讀取多個配置文件====****/ 
    //這樣就會把兩個配置文件作為一個來使用,表面上看是作為兩個使用的 
    //其實內(nèi)部是作為一個使用的,所以在多個配置文件中,里面的id不能重復(fù) 
    //但是name屬性可以重復(fù),就好像人的身份證編號和名字的區(qū)別是一樣的 
    //String[] configLocations = new String[]{"applicationContext.xml", "applicationContext-editor.xml"}; 
    //factory = new ClassPathXmlApplicationContext(configLocations); 
     
    /****=====利用 * 匹配模式讀取多個配置文件====****/ 
    //業(yè)界流行的一句話:約定優(yōu)于配置 
    //所以說當(dāng)有了一個統(tǒng)一的比較好的約定之后,就可以利用框架提供的功能,減少配置量 
    //另外:如果沒有讀取到applicationContext-*.xml,此時即便存在applicationContext.xml,它也不會讀的 
    factory = new ClassPathXmlApplicationContext("applicationContext-*.xml"); 
  } 
 
  /** 
   * 該方法演示的是常見屬性的注入,包括int,String,Array,list,set,map,Date的注入 
   * @see 其中Date類型的注入則是借助了Spring屬性編輯器來實現(xiàn)的 
   */ 
  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()); 
  } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實現(xiàn)圖片模糊效果詳解

    Java實現(xiàn)圖片模糊效果詳解

    圖片模糊是圖像處理中的一種常見效果,它通過平均周圍像素的顏色來使圖像變得模糊,下面我們來看看如何使用Swing庫實現(xiàn)圖片模糊效果吧
    2025-02-02
  • SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory'or?'sqlSessionTemplate'?are?required

    SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory&a

    這篇文章主要介紹了SpringBoot3整合MyBatis報錯:Property?‘sqlSessionFactory‘?or?‘sqlSessionTemplate‘?are?required,其實不是個大問題,只是自己編碼時遇到了,然后總結(jié)總結(jié)分享一下,如果有遇到類似問題的,可以參考一下
    2022-11-11
  • 在Spring Boot中加載初始化數(shù)據(jù)的實現(xiàn)

    在Spring Boot中加載初始化數(shù)據(jù)的實現(xiàn)

    這篇文章主要介紹了在Spring Boot中加載初始化數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 通過實例解析spring對象生命周期

    通過實例解析spring對象生命周期

    這篇文章主要介紹了通過實例解析spring對象生命周期,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • IDEA快速顯示Run DashBoard的圖文詳解

    IDEA快速顯示Run DashBoard的圖文詳解

    這篇文章主要介紹了IDEA快速顯示Run DashBoard的圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Java畢業(yè)設(shè)計實戰(zhàn)之醫(yī)院心理咨詢問診系統(tǒng)的實現(xiàn)

    Java畢業(yè)設(shè)計實戰(zhàn)之醫(yī)院心理咨詢問診系統(tǒng)的實現(xiàn)

    這是一個使用了java+Spring+Maven+mybatis+Vue+mysql開發(fā)的醫(yī)院心理咨詢問診系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習(xí),具有心理咨詢問診該有的所有功能,感興趣的朋友快來看看吧
    2022-01-01
  • Spring集成MyBatis?及Aop分頁的實現(xiàn)代碼

    Spring集成MyBatis?及Aop分頁的實現(xiàn)代碼

    這篇文章主要介紹了Spring集成MyBatis?及Aop分頁的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • SpringSecurity安全管理開發(fā)過程

    SpringSecurity安全管理開發(fā)過程

    Spring?是一個非常流行和成功的?Java?應(yīng)用開發(fā)框架,Spring?Security?基于?Spring?框架,提供了一套?Web?應(yīng)用安全性的完整解決方案,這篇文章主要介紹了SpringSecurity安全管理,需要的朋友可以參考下
    2024-07-07
  • Java使用TCP實現(xiàn)數(shù)據(jù)傳輸實例詳解

    Java使用TCP實現(xiàn)數(shù)據(jù)傳輸實例詳解

    這篇文章主要介紹了Java使用TCP實現(xiàn)數(shù)據(jù)傳輸實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Kotlin與Java的主客觀對比分析

    Kotlin與Java的主客觀對比分析

    這篇文章主要介紹了Kotlin與Java的主客觀對比分析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12

最新評論

宾川县| 石景山区| 岳阳市| 南汇区| 富阳市| 广汉市| 交城县| 萍乡市| 白玉县| 白沙| 香河县| 名山县| 班戈县| 田阳县| 牟定县| 巴彦县| 民丰县| 益阳市| 沁水县| 正安县| 石狮市| 毕节市| 铁岭市| 凌云县| 德钦县| 长宁县| 于田县| 宜川县| 济源市| 宁强县| 府谷县| 金昌市| 施甸县| 麻栗坡县| 万源市| 福贡县| 文安县| 扎囊县| 通江县| 噶尔县| 乐亭县|