Spring.Net
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>
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。