首页 > 代码库 > 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