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

Spring Bean基本管理實(shí)例詳解

 更新時(shí)間:2015年10月20日 14:21:53   作者:hacker0825  
這篇文章主要介紹了Spring Bean基本管理,以實(shí)例形式較為詳細(xì)的分析了Spring Bean的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Spring Bean基本管理。分享給大家供大家參考,具體如下:

一、使用setter方式完成依賴注入

下面是Bean和beans-config.xml文件。

public class HelloBean { 
  private String helloWord; 
  //...省略getter、setter方法   
}

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="helloBean" 
     class="onlyfun.caterpillar.HelloBean"> 
    <property name="helloWord"> 
      <value>Hello!Justin!</value> 
    </property> 
  </bean> 
</beans>
public class SpringDemo { 
  public static void main(String[] args) { 
    Resource rs = new FileSystemResource("beans-config.xml"); 
    BeanFactory factory = new XmlBeanFactory(rs); 
    HelloBean hello = (HelloBean) factory.getBean("helloBean"); 
    System.out.println(hello.getHelloWord()); 
  } 
}

二、使用constructor方式完成注入

public class HelloBean { 
  private String name; 
  private String helloWord; 
  // 建議有要無參數(shù)建構(gòu)方法 
  public HelloBean() { 
  } 
  public HelloBean(String name, String helloWord) { 
    this.name = name; 
    this.helloWord = helloWord; 
  } 
  //...省略getter、setter方法   
}

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="helloBean" 
     class="onlyfun.caterpillar.HelloBean"> 
    <constructor-arg index="0"> 
      <value>Justin</value> 
    </constructor-arg> 
    <constructor-arg index="1"> 
      <value>Hello</value> 
    </constructor-arg> 
  </bean> 
</beans>

public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("beans-config.xml"); 
    HelloBean hello = (HelloBean) context.getBean("helloBean"); 
    System.out.print("Name: "); 
    System.out.println(hello.getName()); 
    System.out.print("Word: "); 
    System.out.println(hello.getHelloWord()); 
  } 
}

三、屬性參考

public class HelloBean { 
  private String helloWord; 
  private Date date; 
  //...省略getter、setter方法   
}

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
    <property name="date"> 
      <ref bean="dateBean"/> 
    </property> 
  </bean> 
</beans>
public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("beans-config.xml"); 
    HelloBean hello = (HelloBean) context.getBean("helloBean"); 
    System.out.print(hello.getHelloWord()); 
    System.out.print(" It's "); 
    System.out.print(hello.getDate()); 
    System.out.println("."); 
  } 
}

四、“byType”自動(dòng)綁定

將“三”中的配置文件改為下面,即可完成bean屬性的按類型自動(dòng)綁定。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

五、“byName”自動(dòng)綁定

將“三”中的配置文件改為下面,即可完成bean屬性的按名稱自動(dòng)綁定。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

六、“constructor”自動(dòng)綁定

將“三”中的配置文件改為下面,即可完成bean屬性的按構(gòu)造方法自動(dòng)綁定。在建立依賴關(guān)系時(shí),Srping容器會(huì)試圖比對(duì)容器中的Bean實(shí)例類型,及相關(guān)的構(gòu)造方法上的參數(shù)類型,看看在類型上是否符合,如果有的話,則選用該構(gòu)造方法來建立Bean實(shí)例。如果無法綁定,則拋出org.springframework.beans.factory.UnsatisfiedDependencyException異常。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

六、“autodetect”自動(dòng)綁定

將“三”中的配置文件改為下面,即可完成bean屬性的自動(dòng)綁定,這個(gè)自動(dòng)綁定是Spring會(huì)嘗試用入constructor來處理依賴關(guān)系的建立,如果不行,則再嘗試用byType類建立依賴關(guān)系。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

七、依賴檢查方式

在自動(dòng)綁定中,由于沒辦法從定義文件中,清楚地看到是否每個(gè)屬性都完成設(shè)定,為了確定某些依賴關(guān)系確實(shí)建立,您可以假如依賴檢查,在<bean>標(biāo)簽使用時(shí)設(shè)定"dependency-check",可以有四種依賴檢查方式:simple、objects、all、none。

simple:只檢查簡(jiǎn)單的類型(像原生數(shù)據(jù)類型或字符串對(duì)象)屬性是否完成依賴關(guān)系,。
objects:檢查對(duì)象類型的屬性是否完成依賴關(guān)系。
all:則檢查全部的屬性是否完成依賴關(guān)系。
none:設(shè)定是默認(rèn)值,表示不檢查依賴性。

<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

八、集合對(duì)象注入

對(duì)于像數(shù)組、List、Set、Map等集合對(duì)象,在注入前必須填充一些對(duì)象至集合中,然后再將集合對(duì)象注入至所需的Bean時(shí),也可以交由Spring的IoC容器來自動(dòng)維護(hù)或生成集合對(duì)象,并完成依賴注入。

public class SomeBean { 
  private String[] someStrArray; 
  private Some[] someObjArray; 
  private List someList; 
  private Map someMap; 
  public String[] getSomeStrArray() { 
    return someStrArray; 
  } 
  public void setSomeStrArray(String[] someStrArray) { 
    this.someStrArray = someStrArray; 
  } 
  public Some[] getSomeObjArray() { 
    return someObjArray; 
  } 
  public void setSomeObjArray(Some[] someObjArray) { 
    this.someObjArray = someObjArray; 
  } 
  public List getSomeList() { 
    return someList; 
  } 
  public void setSomeList(List someList) { 
    this.someList = someList; 
  } 
  public Map getSomeMap() { 
    return someMap; 
  } 
  public void setSomeMap(Map someMap) { 
    this.someMap = someMap; 
  } 
}
public class Some { 
  private String name; 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public String toString() { 
    return name; 
  } 
}

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="some1" class="onlyfun.caterpillar.Some"> 
    <property name="name"> 
      <value>Justin</value> 
    </property> 
  </bean> 
  <bean id="some2" class="onlyfun.caterpillar.Some"> 
    <property name="name"> 
      <value>momor</value> 
    </property> 
  </bean> 
  <bean id="someBean" class="onlyfun.caterpillar.SomeBean"> 
    <property name="someStrArray"> 
      <list> 
        <value>Hello</value> 
        <value>Welcome</value> 
      </list> 
    </property> 
    <property name="someObjArray"> 
      <list> 
         <ref bean="some1"/> 
         <ref bean="some2"/> 
      </list> 
    </property> 
    <property name="someList"> 
      <list> 
         <value>ListTest</value> 
         <ref bean="some1"/> 
         <ref bean="some2"/> 
      </list> 
    </property> 
    <property name="someMap"> 
      <map> 
         <entry key="MapTest"> 
           <value>Hello!Justin!</value> 
         </entry> 
         <entry key="someKey1"> 
           <ref bean="some1"/> 
         </entry> 
      </map> 
    </property> 
  </bean> 
</beans>

public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext( 
          "beans-config.xml"); 
    SomeBean someBean = 
      (SomeBean) context.getBean("someBean"); 
    // 取得數(shù)組型態(tài)依賴注入對(duì)象 
    String[] strs = 
      (String[]) someBean.getSomeStrArray(); 
    Some[] somes = 
      (Some[]) someBean.getSomeObjArray(); 
    for(int i = 0; i < strs.length; i++) { 
      System.out.println(strs[i] + "," 
          + somes[i].getName()); 
    } 
    // 取得List型態(tài)依賴注入對(duì)象 
    System.out.println(); 
    List someList = (List) someBean.getSomeList(); 
    for(int i = 0; i < someList.size(); i++) { 
      System.out.println(someList.get(i)); 
    } 
    // 取得Map型態(tài)依賴注入對(duì)象 
    System.out.println(); 
    Map someMap = (Map) someBean.getSomeMap(); 
    System.out.println(someMap.get("MapTest")); 
    System.out.println(someMap.get("someKey1")); 
  } 
}

希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 解決springboot接入springfox-swagger2遇到的一些問題

    解決springboot接入springfox-swagger2遇到的一些問題

    這篇文章主要介紹了解決springboot接入springfox-swagger2遇到的一些問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 如何解決SpringMVC不能訪問html頁(yè)面

    如何解決SpringMVC不能訪問html頁(yè)面

    這篇文章主要介紹了如何解決SpringMVC不能訪問html頁(yè)面問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Hystrix?Turbine聚合監(jiān)控的實(shí)現(xiàn)詳解

    Hystrix?Turbine聚合監(jiān)控的實(shí)現(xiàn)詳解

    微服務(wù)架構(gòu)下,?個(gè)微服務(wù)往往部署多個(gè)實(shí)例,如果每次只能查看單個(gè)實(shí)例的監(jiān)控,就需要經(jīng)常切換很不?便,在這樣的場(chǎng)景下,我們可以使??Hystrix?Turbine?進(jìn)?聚合監(jiān)控,它可以把相關(guān)微服務(wù)的監(jiān)控?cái)?shù)據(jù)聚合在?起,便于查看
    2022-09-09
  • eclipse 如何創(chuàng)建 user library 方法詳解

    eclipse 如何創(chuàng)建 user library 方法詳解

    這篇文章主要介紹了eclipse 如何創(chuàng)建 user library 方法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 在Java中如何避免創(chuàng)建不必要的對(duì)象

    在Java中如何避免創(chuàng)建不必要的對(duì)象

    作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,但如何才能避免創(chuàng)建不必要的對(duì)象呢?這需要我們好好學(xué)習(xí),這篇文章主要給大家介紹了關(guān)于在Java中如何避免創(chuàng)建不必要對(duì)象的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • Spark?SQL配置及使用教程

    Spark?SQL配置及使用教程

    SparkSQL是spark的一個(gè)模塊,主入口是SparkSession,將SQL查詢與Spark程序無縫混合,這篇文章主要介紹了Spark?SQL配置及使用,需要的朋友可以參考下
    2021-12-12
  • 源碼分析Java中ThreadPoolExecutor的底層原理

    源碼分析Java中ThreadPoolExecutor的底層原理

    這篇文章主要帶大家從源碼分析一下Java中ThreadPoolExecutor的底層原理,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
    2023-05-05
  • SpringCloud hystrix服務(wù)降級(jí)概念介紹

    SpringCloud hystrix服務(wù)降級(jí)概念介紹

    什么是服務(wù)降級(jí)?當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實(shí)際業(yè)務(wù)情況及流量,對(duì)一些服務(wù)和頁(yè)面有策略的不處理或換種簡(jiǎn)單的方式處理,從而釋放服務(wù)器資源以保證核心交易正常運(yùn)作或高效運(yùn)作
    2022-09-09
  • 關(guān)于Java 項(xiàng)目封裝sqlite連接池操作持久化數(shù)據(jù)的方法

    關(guān)于Java 項(xiàng)目封裝sqlite連接池操作持久化數(shù)據(jù)的方法

    這篇文章主要介紹了Java 項(xiàng)目封裝sqlite連接池操作持久化數(shù)據(jù)的方法,文中給大家介紹了sqlite的體系結(jié)構(gòu)及封裝java的sqlite連接池的詳細(xì)過程,需要的朋友可以參考下
    2021-11-11
  • Maven項(xiàng)目讀取resources文件路徑問題解決方案

    Maven項(xiàng)目讀取resources文件路徑問題解決方案

    這篇文章主要介紹了Maven項(xiàng)目讀取resources文件路徑問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論

兖州市| 江华| 阿荣旗| 社旗县| 肇东市| 乌什县| 龙江县| 本溪| 米泉市| 科尔| 红河县| 历史| 湄潭县| 潢川县| 北辰区| 禄劝| 开鲁县| 洱源县| 民丰县| 九寨沟县| 龙胜| 伊宁县| 蓬莱市| 黑龙江省| 天镇县| 墨竹工卡县| 武强县| 惠来县| 都兰县| 广东省| 乐清市| 罗平县| 迭部县| 崇信县| 汤原县| 明溪县| 伊金霍洛旗| 昌图县| 吉木萨尔县| 双鸭山市| 惠东县|