[Spring.NET IoC] 之四:配置補充

作者: Q.yuhen  來源: Q.yuhen  發布時間: 2008-08-17 23:05  閱讀: 4478 次  推薦: 0   原文鏈接   [收藏]  
1. 別名
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net
         http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, Learn.CUI" />
  <alias alias="HelloWorld2" name="HelloWorld"/>
</objects>

我們為 HelloWorld 創建了一個別名 HelloWorld2,我們同樣可以通過 HelloWorld2 獲取對象。請注意下面的測試代碼輸出結果。
object o = context.GetObject("HelloWorld");
object o2 = context.GetObject("HelloWorld2");
Console.WriteLine(object.ReferenceEquals(o, o2)); // output: true

2. 繼承

下面的例子中,我們為 HelloWorld 添加了一個 "parent" 聲明,該申明表示 HelloWorld 繼承 test 的配置屬性,包括構造參數和屬性設置,但不包括 "singleton" 等。當然繼承的僅僅是配置信息,而不是類型。
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net
         http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="test" type="ConsoleApplication1.SpringNet.Test, Learn.CUI" singleton="false">
    <constructor-arg name="s" value="abc..." />
  </object>
  
  <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, Learn.CUI" parent="test" />
</objects>

3. 內聯

對于下面的配置文件,我們還可以改成內聯方式。
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net
         http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, Learn.CUI">
    <constructor-arg name="data" ref="MyData" />
  </object>

  <object id="MyData" type="ConsoleApplication1.SpringNet.MyData, Learn.CUI" />
</objects>

修改結果
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net
         http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, Learn.CUI">
    <constructor-arg name="data">
      <object type="ConsoleApplication1.SpringNet.MyData, Learn.CUI" />
    </constructor-arg>
  </object>

</objects>

4. 空值

注意空值(null) 和空字符串("")不同。
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.net
         http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, Learn.CUI">
    <constructor-arg name="s">
      <null/>
    </constructor-arg>
  </object>
</objects>
0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()