[Spring.NET IoC] 之四:配置补充
1. 别名
我们为 HelloWorld 创建了一个别名 HelloWorld2,我们同样可以通过 HelloWorld2 获取对象。请注意下面的测试代码输出结果。
2. 继承
下面的例子中,我们为 HelloWorld 添加了一个 "parent" 声明,该申明表示 HelloWorld 继承 test 的配置属性,包括构造参数和属性设置,但不包括 "singleton" 等。当然继承的仅仅是配置信息,而不是类型。
3. 内联
对于下面的配置文件,我们还可以改成内联方式。
修改结果
4. 空值
注意空值(null) 和空字符串("")不同。
原作者:http://www.rainsts.net/article.asp?id=257
<?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>
原作者:http://www.rainsts.net/article.asp?id=257
浙公网安备 33010602011771号