Spring.Net 本质就是一个抽象工厂。容器(IApplicationContext)通过配置创建一个对象。
1.创建对象(IoC)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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"> <description>An example that demonstrates simple IoC features.</description> </objects> </spring> </configuration>
自动提示功能可通过schema文件约束:spring-objects-1.3.xsd
在object节点导入命名空间即可。
初始化容器对象:
IApplicationContext ctx = ContextRegistry.GetContext();
需要引用2个程序集:Common.Logging.dll和Spring.Core.dll
配置一个类型:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<objects xmlns="http://www.springframework.net"> <description>An example that demonstrates simple IoC features.</description> <object name="{调用名}" type="{类的限定名}, {项目名}"> </object> </objects>
实例化:
ctx.GetObject("{调用的名字}")
2.依赖注入(DI)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<objects xmlns="http://www.springframework.net"> <object name="MyMovieLister" type="Spring.Examples.MovieFinder.MovieLister, Spring.Examples.MovieFinder"> <property name="movieFinder" ref="MyMovieFinder"/> </object> <object name="MyMovieFinder" type="Spring.Examples.MovieFinder.SimpleMovieFinder, Spring.Examples.MovieFinder"/> </object> </objects>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<object name="AnotherMovieFinder" type="Spring.Examples.MovieFinder.ColonDelimitedMovieFinder, Spring.Examples.MovieFinder"> <constructor-arg index="0" value="movies.txt"/> </object>