[Spring.NET IoC] 之四:配置補充
1. 別名
我們為 HelloWorld 創建了一個別名 HelloWorld2,我們同樣可以通過 HelloWorld2 獲取對象。請注意下面的測試代碼輸出結果。
2. 繼承
下面的例子中,我們為 HelloWorld 添加了一個 "parent" 聲明,該申明表示 HelloWorld 繼承 test 的配置屬性,包括構造參數和屬性設置,但不包括 "singleton" 等。當然繼承的僅僅是配置信息,而不是類型。
3. 內聯
對于下面的配置文件,我們還可以改成內聯方式。
修改結果
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" />
<alias alias="HelloWorld2" name="HelloWorld"/>
</objects>
<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
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>
<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>
<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>
<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>
<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>
全站熱搜