码迷,mamicode.com
首页 > 编程语言 > 详细

Spring.Net

时间:2014-10-08 01:27:44      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   strong   

 

Spring.Net 本质就是一个抽象工厂。容器(IApplicationContext)通过配置创建一个对象。

1.创建对象(IoC)

bubuko.com,布布扣
<?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

 

配置一个类型:

bubuko.com,布布扣
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description>
<object name="{调用名}" type="{类的限定名}, {项目名}">
</object>
</objects>
配置一个待实例化的类

 

实例化:

ctx.GetObject("{调用的名字}") 

 

2.依赖注入(DI)

bubuko.com,布布扣
<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>
属性注入
bubuko.com,布布扣
  <object name="AnotherMovieFinder"      type="Spring.Examples.MovieFinder.ColonDelimitedMovieFinder, Spring.Examples.MovieFinder">
    <constructor-arg index="0" value="movies.txt"/>
  </object>
构造器参数注入

 

Spring.Net

标签:des   style   blog   http   color   io   os   ar   strong   

原文地址:http://www.cnblogs.com/neverc/p/4010314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!