[Spring.NET IoC] 之二:配置文件

Spring.NET IoC 支持2种配置文件方式:

1. 应用程序配置文件

app.config / web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net">
      <object id="HelloWorld" type="ConsoleApplication1.SpringNet.HelloWorld, Learn.CUI" >
      </object>
    </objects>

  </spring>
</configuration>

test.cs
      IApplicationContext context = ContextRegistry.GetContext();
      object o = context.GetObject("HelloWorld");

2. 独立配置文件

springtest.xml
<?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">
  </object>
</objects>

test.cs
IApplicationContext context = new XmlApplicationContext(@"springtest.xml");
object o = context.GetObject("HelloWorld");

建议使用独立的配置文件,应用程序配置文件已经被塞入太多内容了。Spring.NET 还支持很多高级的配置文件功能,将在后面做进一步介绍。

你可能感兴趣的:(spring)