首页 > 代码库 > Spring.Net
Spring.Net
Spring.Net 本质就是一个抽象工厂。容器(IApplicationContext)通过配置创建一个对象。
1.创建对象(IoC)
<?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>
初始化容器对象:
IApplicationContext ctx = ContextRegistry.GetContext();
需要引用2个程序集:Common.Logging.dll和Spring.Core.dll
配置一个类型:
<objects xmlns="http://www.springframework.net"><description>An example that demonstrates simple IoC features.</description><object name="{调用名}" type="{类的限定名}, {项目名}"></object></objects>
实例化:
ctx.GetObject("{调用的名字}")
2.依赖注入(DI)
<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>
<object name="AnotherMovieFinder" type="Spring.Examples.MovieFinder.ColonDelimitedMovieFinder, Spring.Examples.MovieFinder"> <constructor-arg index="0" value=http://www.mamicode.com/"movies.txt"/> </object>
Spring.Net
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。