1. 程式人生 > >Spring Xml 檔案的配置 引數 屬性 說明

Spring Xml 檔案的配置 引數 屬性 說明

1、value元素

<value/>元素通過字串來指定屬性或構造器引數的值。

<bean id="myDataSource" detroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName">
  <value>com.mysql.jdbc.Driver</value>
 </proerpty>
 <property name="url">
  <value>jdbc:mysql://localhost:3306/mydb</value>
 </property>
 <property name="username">
  <vlaue>root</value>
 </property>
</bean>

2、idref元素

idref元素用來將容器內其它bean的id傳給<constructor-arg/>或<property/>元素,同時提供錯誤難功能。

<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
 <property name="targetName">
  <idref bean="theTargetBean" />
 </property>
</bean>

等同於:

<bean id="theTargetBean" class="..." />
<bean id="theClientBean" class="...">
 <property name="targetName">
  <value>theTargetBean</value>
 </property>
</bean>

使用idref標記允許容器在部署時驗證所被引用的bean是否存在。此外,如果被引用的bean在同一XML檔案內,且bean名字就是bean id,那麼可以使用local屬性。
此屬性允許XML解析器在解析XML檔案時來對引用的bean進行驗證。

<property name="targetName">
 <idref local="theTargetBean" />
</property>

3、ref元素

形式一:<ref bean="someBean">
這是最常見的形式是通過使用ref標記指定bean屬性的目標bean,通過該標籤可以引用同一容器或父容器內的任何bean(無論是否在同一XML檔案中)。
XML‘bean’元素的值即可以是指定的bean的id值也可以是其name值。

形式二:<ref local="someBean">
使用ref的local屬性指定目標bean,它可以利用XML解析器來難所引用的bean是否存在同一檔案中。local屬性值必須是目標bean的id屬性值。

形式三:<bean parent="someBean">
通過使用ref的parent屬性來引用當前視窗的父容器中的bean。parent屬性值即可以是目標bean的id值,也可以是name屬性值。

4、內部 bean:

所 謂內部bean(inner bean)是指在一個bean的<property/>或<constructor-arg/>中使用< bean/>元素定義的bean.內部bean不需要有id或name屬性,即使有也會被視窗忽略.
內部bean總是匿名的且它們總是prototype模式的.同時將內部bean注入到包含該內部bean之外的bean是不可能的.
<bean id="outer" class="...">
 <property name="target">
  <bean class="com.mycoompany.Person">
   <property name="name" value="Fiona Apple"/>
   <property name="age" value="25"/>
  </bean>
 </property>
</bean>

5、集合合併:

從Spring2.0開始,Spring IoC容器將支援集合的合併。父子集合元素合併後的值就是子集合中的最終結果,而且子集合中的元素值將覆蓋父集合中的對應的值。
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
 <property name="adminEmails">
  <props>
   <prop key="administrator">[email protected]</prop>
   <prop key="support">[email protected]</prop>
  </props>
 </property>
</bean>
<bean id="child" parent="parent">
 <property name="adminEmails">
  <props merge="trur">
   <prop key="sales">[email protected]</prop>
   <prop key="support">[email protected]</prop>
  </props>
 </property>
</bean>
</beans>

合併後內容:

list集合有排序功能,父bean的列表內容將排在子bean列表內容的前面;
merge屬性必須在繼承的子bean中定義。

6、Nulls

<null/>用於處理null值。Spring會把屬性的空引數當作空字串處理。

<bean class="ExampleBean">
 <property name="email">
  <value></value>
 </property>
</bean>

等同於

excapleBean.setEamil("");

而null值則可以使用<null/>元素來表示:

<bean class="ExampleBean">
 <property name="email"><null/></property>
</bean>


7、簡寫:

針對常見的value值或bean的引用,Spring提供了簡化格式用於替代<value/>和<ref/>元素。
<property/>、<constructor-arg/>、<entry/>元素都支援value屬性,它可以用來替代內嵌的<value/>元素。

<property name="myProperty">                           
 <value>hello</value>           =====            <property name="myProperty" value="helo" />
</property>

<constructor-arg>
 <value>hello</value>           =====            <constructor-arg value="hello" />
</construtctor-arg>

<entry key="myKey">
 <value>hello</value>           =====            <entry key="myKey" value="hello" />
</entry>


<property/>和<constructor-arg/>支援類似的簡寫屬性ref,它可以替找整個內嵌的</ref>元素。

<property name="myProperty">
 <ref bean="myBean">            =====            <property name="myProperty" ref="myBean" />
</property>

<constructor-arg>
 <ref bean="myBean">            =====            <constructor-arg ref="myBean" />
</constructor-arg>

切記:儘管存在等同於<ref bean="xxx" >元素的簡寫形式,但並沒有<ref local="xxx">的簡寫形式。

map中的entry元素的簡寫形式為key/key-ref和value/value-ref屬性。

<entry>
 <key>
  <ref bean="myKeyBean" />            =====          <entry key-ref="myKeyBean" value-ref="myValueBean" />
 </key>
 <ref bean="myValueBean" />
</entry>

8、組合屬性名稱

當設定bean的組合屬性時,除了最後一下屬性外,只要其他屬性值不為null,組合或巢狀屬性名是完全合法的。

<bean id="foo" class="foo.Bar">
 <property name="fred.bob.sammy" value="123" />
</bean>

9、depends-on屬性:

depends-on屬性可以用於當前bean初始化之前顯式的強制一個或多個bean被初始化。

<bean id="beanOne" class="ExampleBean" depends-on="manager">
 <property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />

若需要表達對多個bean的依賴,可民認在<depends-on />中將指定的多個bean名字用分隔符進行分隔,分隔符可以是逗號、空格及分號等。

<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
 <property name="manager" ref="manager" />
</bean>
<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />

10、延遲初始化bean--lazy-init 屬性:

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true">
 <!-- various properties here... -->
</bean>
<bean name="noo.lazy" class="com.foo.AnotherBean">
 <!-- various properties here... -->
</bean>

如 果一個bean被設定為延遲初始化,而另一個非延遲初始化的singleton bean依賴於它,那麼當ApplicationContext提前例項化singleton bean時,它必須也確保所有上述singleton依賴bean也被預先初始化,當然也包括設定為延遲例項化的bean.

在容器層次中通過在<beans />元素上使用‘default-lazy-init’屬性來控制延遲初始化也是可能的。
<beans default-lazy-init="true">
 <!-- no beans will be eagerly pre-instantiated... -->
</beans>

11、autowire<自動裝配> 屬性:

模式     說明

no      不使用自動裝配,必須通過ref元素指定依賴,這是預設設定。

byName     根據屬性名自動裝配。Spring將檢查容器並根據名字查詢與屬性完全一致的bean,並將其與屬性自動裝配。

byType     如果容器中存在一個與指定屬性型別相同的bean,那麼將與該屬性自動裝配。如果存在多個,則丟擲異常。

constructor    與byType的方式類似,不同之處在於它應用於構造器引數。如果在容器中未找到與構造器引數型別一致的bean,那麼將丟擲異常。

autodetect    通過bean類的自省機制(introspection)來決定是使用constructor還是byType方式進行自動裝配。如果發現預設的構造器,那麼將使用byType方式。


--通過設定<bean />元素的autowire-candidate="false",可以針對單個bean設定其是否為被自動裝配物件。

12、dependency-check <依賴檢查> 屬性:

此屬性用於檢查bean定義中實際屬性值的設定。

模式     說明

none     沒有依賴檢查,如果bean的屬性沒有值的話可以不用設定。

simple     對於原始型別及集合(除協作者外的一切東西)執行依賴檢查。

object     僅對協作者執行依賴檢查員。

all      對協作者,原始型別及集合執行依賴檢查。

3.2.2. 例項化容器

Spring IoC容器的例項化非常簡單,如下面的例子:
Resource resource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
... 或...
ClassPathResource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
... 或...
ApplicationContext context = new ClassPathXmlApplicationContext(
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) context;

Table 3.1. bean定義

名稱連結