您的位置:知识库 » 软件测试

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

作者: Q.yuhen  来源: Q.yuhen  发布时间: 2008-08-17 23:05  阅读: 4533 次  推荐: 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
 

软件测试热门文章

    软件测试最新文章

      最新新闻

        热门新闻