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

詳解Java的Spring框架下bean的自動裝載方式

 更新時間:2015年12月08日 08:54:27   投稿:goldensun  
這篇文章主要介紹了Java的Spring框架下bean的自動裝載方式,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下

Spring容器可以自動裝配相互協(xié)作bean之間的關(guān)系,這有助于減少對XML配置,而無需編寫一個大的基于Spring應(yīng)用程序的較多的<constructor-arg>和<property>元素。

自動裝配模式:
有下列自動裝配模式,可用于指示Spring容器使用自動裝配依賴注入。使用<bean/>元素的autowire屬性為一個bean定義中指定自動裝配模式。

byName模式
這種模式規(guī)定由自動裝配屬性名稱。Spring容器在外觀上自動線屬性設(shè)置為byName的XML配置文件中的bean。然后,它嘗試匹配和接線其屬性與配置文件中相同的名稱定義的Bean。如果找到匹配項(xiàng),它會注入這些bean,否則,它會拋出異常。

例如,如果一個bean定義設(shè)置為自動裝配byName的配置文件,它包含aspellChecker屬性(即,它有一個 setSpellChecker(...)方法),Spring就會查找名為拼寫檢查一個bean定義,并用它來設(shè)置該屬性。仍然可以使用的<property>標(biāo)簽連線其余屬性。下面的例子將說明這個概念。

來創(chuàng)建一個Spring應(yīng)用程序:
這里是TextEditor.java文件的內(nèi)容:

package com.yiibai;

public class TextEditor {
  private SpellChecker spellChecker;
  private String name;

  public void setSpellChecker( SpellChecker spellChecker ){
   this.spellChecker = spellChecker;
  }
  public SpellChecker getSpellChecker() {
   return spellChecker;
  }

  public void setName(String name) {
   this.name = name;
  }
  public String getName() {
   return name;
  }

  public void spellCheck() {
   spellChecker.checkSpelling();
  }
}

下面是另外一個相關(guān)的類文件SpellChecker.java內(nèi)容:

package com.yiibai;

public class SpellChecker {
  public SpellChecker() {
   System.out.println("Inside SpellChecker constructor." );
  }

  public void checkSpelling() {
   System.out.println("Inside checkSpelling." );
  }
  
}

以下是MainApp.java文件的內(nèi)容:

package com.yiibai;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
  public static void main(String[] args) {
   ApplicationContext context = 
       new ClassPathXmlApplicationContext("Beans.xml");

   TextEditor te = (TextEditor) context.getBean("textEditor");

   te.spellCheck();
  }
}

以下是在正常情況下的配置文件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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- Definition for textEditor bean -->
  <bean id="textEditor" class="com.yiibai.TextEditor">
   <property name="spellChecker" ref="spellChecker" />
   <property name="name" value="Generic Text Editor" />
  </bean>

  <!-- Definition for spellChecker bean -->
  <bean id="spellChecker" class="com.yiibai.SpellChecker">
  </bean>

</beans>

但是,如果要使用自動裝配“byName”,那么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-3.0.xsd">

  <!-- Definition for textEditor bean -->
  <bean id="textEditor" class="com.yiibai.TextEditor" 
   autowire="byName">
   <property name="name" value="Generic Text Editor" />
  </bean>

  <!-- Definition for spellChecker bean -->
  <bean id="spellChecker" class="com.yiibai.SpellChecker">
  </bean>

</beans>

創(chuàng)建源代碼和bean配置文件完成后,讓我們運(yùn)行應(yīng)用程序。如果一切順利,這將打印以下信息:

Inside SpellChecker constructor.
Inside checkSpelling.

byType模式
式規(guī)定由自動裝配屬性類型。Spring容器在外觀上autowire屬性設(shè)置為byType的XML配置文件中的bean。然后,它嘗試匹配和連接一個屬性,如果它的類型有完全相同的豆子名稱的一個匹配的配置文件。如果找到匹配項(xiàng),它會注入這些bean,否則,它會拋出異常。

例如,如果一個bean定義設(shè)置為自動裝配byType的配置文件,它包含拼寫檢查類型的aspellChecker屬性,春季查找名為拼寫檢查一個bean定義,并用它來設(shè)置該屬性。仍然可以使用<property>標(biāo)簽接線其余屬性。下面的例子將說明這個概念,會發(fā)現(xiàn)和上面的例子沒有什么區(qū)別,除了XML配置文件已被更改。

同樣,來創(chuàng)建一個Spring應(yīng)用程序說明:
這里是TextEditor.java文件的內(nèi)容:

package com.yiibai;

public class TextEditor {
  private SpellChecker spellChecker;
  private String name;

  public void setSpellChecker( SpellChecker spellChecker ) {
   this.spellChecker = spellChecker;
  }
  public SpellChecker getSpellChecker() {
   return spellChecker;
  }

  public void setName(String name) {
   this.name = name;
  }
  public String getName() {
   return name;
  }

  public void spellCheck() {
   spellChecker.checkSpelling();
  }
}

下面是另外一個相關(guān)的類文件SpellChecker.java內(nèi)容:

package com.yiibai;

public class SpellChecker {
  public SpellChecker(){
   System.out.println("Inside SpellChecker constructor." );
  }

  public void checkSpelling() {
   System.out.println("Inside checkSpelling." );
  }
  
}

以下是MainApp.java文件的內(nèi)容:

package com.yiibai;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
  public static void main(String[] args) {
   ApplicationContext context = 
       new ClassPathXmlApplicationContext("Beans.xml");

   TextEditor te = (TextEditor) context.getBean("textEditor");

   te.spellCheck();
  }
}

以下是在正常情況下的配置文件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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- Definition for textEditor bean -->
  <bean id="textEditor" class="com.yiibai.TextEditor">
   <property name="spellChecker" ref="spellChecker" />
   <property name="name" value="Generic Text Editor" />
  </bean>

  <!-- Definition for spellChecker bean -->
  <bean id="spellChecker" class="com.yiibai.SpellChecker">
  </bean>

</beans>

但是,如果要使用自動裝配“byType”,那么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-3.0.xsd">

  <!-- Definition for textEditor bean -->
  <bean id="textEditor" class="com.yiibai.TextEditor" 
   autowire="byType">
   <property name="name" value="Generic Text Editor" />
  </bean>

  <!-- Definition for spellChecker bean -->
  <bean id="SpellChecker" class="com.yiibai.SpellChecker">
  </bean>

</beans>

當(dāng)創(chuàng)建源代碼和bean配置文件完成后,讓我們運(yùn)行應(yīng)用程序。如果一切順利,這將打印以下信息:

Inside SpellChecker constructor.
Inside checkSpelling.

由構(gòu)造函數(shù)自動裝配
這種模式是非常相似byType,但它應(yīng)用于構(gòu)造器參數(shù)。 Spring容器在外觀上autowire屬性被設(shè)置XML配置文件中bean的。然后,它嘗試匹配和連線它的構(gòu)造函數(shù)的參數(shù)與bean名稱的配置文件只有一個。如果找到匹配項(xiàng),它會注入這些bean,否則,它會拋出異常。

例如,如果一個bean定義設(shè)置為通過構(gòu)造配置文件自動裝配,它具有與拼寫檢查類型的參數(shù)之一的構(gòu)造函數(shù),春天尋找一個bean定義namedSpellChecker,并用它來設(shè)置構(gòu)造函數(shù)的參數(shù)。仍然可以使用<constructor-arg>標(biāo)簽連線剩余的參數(shù)。下面的例子將說明這個概念。
這里是TextEditor.java文件的內(nèi)容:

package com.yiibai;

public class TextEditor {
  private SpellChecker spellChecker;
  private String name;

  public TextEditor( SpellChecker spellChecker, String name ) {
   this.spellChecker = spellChecker;
   this.name = name;
  }
  public SpellChecker getSpellChecker() {
   return spellChecker;
  }
  public String getName() {
   return name;
  }

  public void spellCheck() {
   spellChecker.checkSpelling();
  }
}

下面是另外一個相關(guān)的類文件SpellChecker.java內(nèi)容:

package com.yiibai;

public class SpellChecker {
  public SpellChecker(){
   System.out.println("Inside SpellChecker constructor." );
  }

  public void checkSpelling()
  {
   System.out.println("Inside checkSpelling." );
  }
  
}

以下是MainApp.java文件的內(nèi)容:

package com.yiibai;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
  public static void main(String[] args) {
   ApplicationContext context = 
       new ClassPathXmlApplicationContext("Beans.xml");

   TextEditor te = (TextEditor) context.getBean("textEditor");

   te.spellCheck();
  }
}

以下是在正常情況下的配置文件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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- Definition for textEditor bean -->
  <bean id="textEditor" class="com.yiibai.TextEditor">
   <constructor-arg ref="spellChecker" />
   <constructor-arg value="Generic Text Editor"/>
  </bean>

  <!-- Definition for spellChecker bean -->
  <bean id="spellChecker" class="com.yiibai.SpellChecker">
  </bean>

</beans>

但是,如果要使用由“構(gòu)造函數(shù)”自動裝配,那么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-3.0.xsd">

  <!-- Definition for textEditor bean -->
  <bean id="textEditor" class="com.yiibai.TextEditor" 
   autowire="constructor">
   <constructor-arg value="Generic Text Editor"/>
  </bean>

  <!-- Definition for spellChecker bean -->
  <bean id="SpellChecker" class="com.yiibai.SpellChecker">
  </bean>

</beans>

創(chuàng)建源代碼和bean配置文件完成后,讓我們運(yùn)行應(yīng)用程序。如果一切順利,這將打印以下信息:

Inside SpellChecker constructor.
Inside checkSpelling.

除此之外,還有autodetect和默認(rèn)方式,這里就不再細(xì)講。
自動裝配的局限性:
自動裝配最好效果是它始終在一個項(xiàng)目中使用。如果自動裝配不一般的使用,它可能會被混淆為開發(fā)人員可以使用它來連接只有一個或兩個bean定義。不過,自動裝配可以顯著減少需要指定屬性或構(gòu)造器參數(shù),但你應(yīng)該使用它們之前考慮自動裝配的局限性和缺點(diǎn)。

201512885234252.png (592×197)

相關(guān)文章

  • Mybatis分解式查詢使用方法

    Mybatis分解式查詢使用方法

    這篇文章主要介紹了Mybatis分解式查詢使用方法,分解式查詢就是將一條Sql語句拆分成多條。在 MyBatis 多表查詢中,使用連接查詢時一個 Sql 語句就可以查詢出所有的數(shù)據(jù)
    2023-04-04
  • SpringBoot +Vue開發(fā)考試系統(tǒng)的教程

    SpringBoot +Vue開發(fā)考試系統(tǒng)的教程

    這篇文章主要介紹了SpringBoot +Vue開發(fā)考試系統(tǒng),支持多種題型:選擇題、多選題、判斷題、填空題、綜合題以及數(shù)學(xué)公式。支持在線考試,教師在線批改試卷。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2020-05-05
  • SpringMvc定制化深入探究原理

    SpringMvc定制化深入探究原理

    SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計模式,請求驅(qū)動類型的輕量級Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦,這篇文章主要介紹了SpringMvc定制化原理
    2022-10-10
  • 詳解SpringBoot中實(shí)現(xiàn)依賴注入功能

    詳解SpringBoot中實(shí)現(xiàn)依賴注入功能

    這篇文章主要介紹了詳解SpringBoot中實(shí)現(xiàn)依賴注入功能,SpringBoot的實(shí)現(xiàn)方式基本都是通過注解實(shí)現(xiàn)的。有興趣的可以了解一下。
    2017-04-04
  • Hutool開發(fā)利器MapProxy類使用技巧詳解

    Hutool開發(fā)利器MapProxy類使用技巧詳解

    這篇文章主要為大家介紹了Hutool開發(fā)利器MapProxy類使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • SpringBoot整合Lettuce redis過程解析

    SpringBoot整合Lettuce redis過程解析

    這篇文章主要介紹了SpringBoot整合Lettuce redis過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • 一文帶你了解Java中的ForkJoin

    一文帶你了解Java中的ForkJoin

    這篇文章主要介紹了一文帶你了解Java中的ForkJoin,F(xiàn)orkJoinTask本身的依賴關(guān)系并不復(fù)雜,它與異步任務(wù)計算FutureTask一樣均實(shí)現(xiàn)了Future接口,下文更多相關(guān)資料,需要的小伙伴可以參考一下
    2022-04-04
  • SpringBoot中GlobalExceptionHandler異常處理機(jī)制詳細(xì)說明

    SpringBoot中GlobalExceptionHandler異常處理機(jī)制詳細(xì)說明

    Spring Boot的GlobalExceptionHandler是一個全局異常處理器,用于捕獲和處理應(yīng)用程序中發(fā)生的所有異常,這篇文章主要給大家介紹了關(guān)于Java中GlobalExceptionHandler異常處理機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • SpringBoot集成redis的示例代碼

    SpringBoot集成redis的示例代碼

    redis想必小伙伴們即使沒有用過,也是經(jīng)常聽到的,在工作中,redis用到的頻率非常高,本文主要介紹了SpringBoot集成redis,感興趣的可以參考一下
    2021-10-10
  • Spring?Boot最經(jīng)典的20道面試題你都會了嗎

    Spring?Boot最經(jīng)典的20道面試題你都會了嗎

    Spring Boot是現(xiàn)代化的Java應(yīng)用程序開發(fā)框架,具有高度的靈活性和可擴(kuò)展性,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot最經(jīng)典的20道面試題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06

最新評論

梁河县| 连平县| 温州市| 临邑县| 河曲县| 麟游县| 永胜县| 普兰店市| 自治县| 甘南县| 韶关市| 大关县| 广德县| 弋阳县| 长沙市| 马龙县| 习水县| 台北市| 博乐市| 梁河县| 齐河县| 临安市| 东至县| 新沂市| 扎兰屯市| 枞阳县| 陇西县| 大荔县| 合阳县| 大埔县| 乐山市| 莒南县| 和田县| 永康市| 萨迦县| 正蓝旗| 策勒县| 岳西县| 娄底市| 敦煌市| 镇远县|