1.通过Spring.NET的IOC容器(IApplicationContext)获取某个类的实例,其IOC容器(IApplicationContext)的配置信息存储在一个标准的.net应用程序配置文件
<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">
<description>相关描述</description>
</objects>
<spring>
2.配置IOC容器(IApplicationContext)将要获取的类(MovieLister)
<objects xmlns="http://www.springframework.net" >
<description>相关描述</description>
<!-- 引用程序集的MovieLister及定义一个MovieLister对象 -->
<object id="MyMovieLister" type="Spring.IocQuickStart.MovieFinder.MovieLister, Spring.IocQuickStart.MovieFinder">
<!-- 设置注入 -->
<property name="MovieFinder" ref="AnotherMovieFinder"/>
</object>
<!-- 配置执行注入的类 -->
<object id="AnotherMovieFinder" type="Spring.IocQuickStart.MovieFinder.ColonDelimitedMovieFinder, Spring.IocQuickStart.MovieFinder">
<constructor-arg index="0" value="movies.txt"/>
</object>
<object type="Spring.Objects.Factory.Attributes.RequiredAttributeObjectPostProcessor, Spring.Core"/>
</objects>
3.获取IOC容器(IApplicationContext)的具体实体
IApplicationContext ctx = ContextRegistry.GetContext();
4.获取配置完毕(MovieLister)类的实例
MovieLister lister = (MovieLister)ctx.GetObject("MyMovieLister");
5.使用该类的(IApplicationContext)MoviesDirectedBy方法进行排序
Movie[] movies = lister.MoviesDirectedBy("Roberto Benigni");
6.使用log4net进行输出
private static readonly ILog LOG = LogManager.GetLogger(typeof(Program));
LOG.Debug("Searching for movie...");