Spring Framework 4.0 官方文檔核心內容整理之 --- XML方式配置Bean

Spring Framework 4.0 官方文檔核心內容整理之 --- XML方式配置Beanjava

Spring框架的核心部分就是Ioc容器,而Ioc控制的就是各類Bean,一個Spring項目的水平每每從其XML配置文件內容就能略知一二,不少項目,每每是外包公司的項目,配置文件每每是亂七八糟,抱着能跑就行,不報錯就行的態度去寫,而後在項目中後期發現各類缺失又去一通亂補,其結果就是,整個文檔可讀性極差,毫無章法。這也不能怪寫這個XML的人,拿着苦逼程序員的工資幹着架構師的工做必然是這個結果。爲了程序員的幸福,我認爲有必要來一套簡單快速的官方文檔核心配置概括整理和解釋,好讓苦逼猿們在工做中能正確快速的提升自身和項目的總體水平。程序員



<bean   class="這個Bean的類"  session

            name/id="Bean在容器裏面的惟一名稱" 架構

            scope="Bean的生命週期,詳細解釋看下面"  框架

            autowiring-mode="這個Bean的properties的自動注入方式,詳細解釋看下面"  this

            lazy-init="是否爲懶加載,詳細解釋看下面"spa

            init-method="容器初始化該Bean後的回調方法,詳細解釋看下面"  prototype

            destroy-method = "容器在銷燬Bean後的回調方法,詳細解釋看下面"    code

            abstract="是否爲抽象Bean,主要用來統一XML配置文檔裏面的不少Bean的屬性配置,與Java的Abstract Class無任何關係"orm

            parent="父Bean的名稱,會繼承父Bean的屬性,也是隻在配置文檔中有效,與Java的Class無任何關係"

            factory-method="工廠方法的名字"

            factory-bean="工廠Bean的名字"

            depends-on ="依賴Bean的名字,Spring保證會在初始化這個Bean前先初始化被依賴的Beans,這個屬性不會被子Bean繼承,子Bean要從新寫本身的depends-on"      

            autowire-candidate = "是否爲自動注入的候選,通常當其餘Bean設置autowiring-mode屬性爲自動搜尋時能夠避免或容許該Bean被列入匹配列表"    

            primary = "是否將該Bean在其餘Bean的自動注入候選人中設爲首選" 

>

            

            // Constructor-arg方式給屬性賦值寫法一

            <constructor-arg type="int" value="7500000"/>

            // Constructor-arg方式給屬性賦值寫法二

            <constructor-arg name="years" value="7500000"/>

            // Constructor-arg方式給屬性賦值寫法三

            <constructor-arg index="0" value="7500000"/>

           

            // Properties方式給屬性賦值寫法一

            <property name="beanOne">

                 <ref bean="另一個Bean的名字"/>

            </property>

            // Properties方式給屬性賦值寫法二

            <property name="beanOne" ref="另一個Bean的名字"/>

            // Properties方式給屬性賦值寫法三

            <property name="integerProperty" value="1"/>

            

       

</bean>



Spring Bean 的XML描述文檔有下列屬性 
      

class

(Bean的Class)

聲明用於生成Bean的Class。

name 

(Bean的名稱)

Bean 在該環境的惟一名稱。 能夠用 id 或 name 來聲明

scope

(Bean的生命週期)

Bean 的模式,Spring 官方文檔描述以下

單例模式(singleton):

  默認的Bean模式,注意區別是Spring的Bean是每一個容器容許一個單例,而不是每一個類加載器一個單例,全部須要被注入的Bean都會獲得該Bean的一個引用。

原型模式 (prototype): 

  每個將該Bean注入其餘Bean中都會注入一個該Bean的新實例。Spring對原型模式的Bean會進行構造,編譯和初始化,可是不會執行銷燬回調方法,就算該Bean定義了銷燬回調方法也不會被Spring執行,須要本身實現Bean Post Processor。

請求模式 (request): 

  該Bean在每個Http請求時都會新生成一個只對應該請求的Bean。只有在該Web應用的上下文中有效。

會話模式 (session):

  該Bean在每個Session的生命週期中存在。只有在該Web應用的上下文中有效。

全局會話模式 (global session) :

  該Bean在每個全局Session的生命週期中存在。通常用在portlet裏。

自定義模式 (custom):

  經過擴展或重寫Spring預約義的Bean Scope。

constructor-arg

(參數注入方法一)

構造方式注入參數 , 能夠按照類型或者序號或者名稱來賦值

例如   

public class ExampleBean {
    
private int years;

private String ultimateAnswer;

public ExampleBean(int years, String ultimateAnswer) { 
this.years = years;
this.ultimateAnswer = ultimateAnswer;

} 
}
經過使用下面的幾種方式 都能完成同樣的事  

<bean id="exampleBean" class="examples.ExampleBean"> 
<constructor-arg type="int" value="7500000"/> 
<constructor-arg type="java.lang.String" value="42"/>
</bean>

         

<bean id="exampleBean" class="examples.ExampleBean"> 
<constructor-arg index="0" value="7500000"/> 
<constructor-arg index="1" value="42"/>
</bean>

<bean id="exampleBean" class="examples.ExampleBean"> 
<constructor-arg name="years" value="7500000"/>
<constructor-arg name="ultimateanswer" value="42"/>
</bean>

   

最後這個方式要修改下Class,添加以下註解

@ConstructorProperties({"years", "ultimateAnswer"})

properties(參數注入方法二)

Setter方式注入參數

public class ExampleBean
{
   private AnotherBean beanOne;
   private YetAnotherBean beanTwo; 
   private int i;

   public void setBeanOne(AnotherBean beanOne) 
   { 
      this.beanOne = beanOne;
   }

   public void setBeanTwo(YetAnotherBean beanTwo) 
   { 
      this.beanTwo = beanTwo;
   }

   public void setIntegerProperty(int i)
   {
      this.i = i;
   }
 
}
<bean id="exampleBean" class="examples.ExampleBean">
   
  // 寫法1
  
  <property name="beanOne">
  <ref bean="anotherExampleBean"/>
  </property>
   
  // 寫法2

  <property name="beanTwo" ref="yetAnotherBean"/> 

  // 寫法3
  <property name="integerProperty" value="1"/>

</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/> 

<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

autowiring mode 屬性自動關聯模式

(項目裏最好就使用默認的No選項,或者直接不寫,若是必定要用,能夠在不須要被關聯Bean裏定義,autowire-candidate=false)

no (默認):

不可被自動關聯,要關聯這個Bean的屬性只能用 ref 元素來配。不建議在大型應用中使用其餘模式。


byName :

Spring會按照這個bean裏的屬性名字去找相應的Bean而後關聯。


byType :

Spring會按照這個bean裏的屬性類型去找相應的Bean而後關聯。若是沒找到,這個屬性就是Null,若是在文檔裏找到了多個同類型的Bean,則會報錯。


constructor :

Spring會在其餘Bean的constructor描述裏找,若是找到多個,則會報錯

lazy-initialization mode

(懶加載模式)

容器對Bean的初始化方式,若是不寫,則默認爲飢渴模式,

若是寫<bean id="xxxxx" class="com.xxx" lazy-init="true"/> 則容器會在第一次須要這個Bean的時候纔去初始化。

initialization method

(初始化後回調方法) 

<bean id="xxx" class="xxx.xxx" init-method="init"/>

Spring會在該Bean初始化完成後執行init方法

public class xxx {

public void init() {
// do some initialization work

} } 


destruction method

(銷燬後回調方法 )

<bean id="xxx" class="xxx.xxx"  destroy-method="cleanup" />

Spring會在該Bean銷燬後執行此方法

public class xxx {

public void cleanup() {

// do some destruction work (like releasing pooled connections) 

} }

相關文章
相關標籤/搜索