• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
多莱特~梦未醒,再出发

行路难,行路难,多歧路,今安在。

长风破浪会有时,直挂云帆济沧海。

博客园    首页    新随笔       管理     

Sping.Net (5)IOC

创建对象的几种方法

The interface IApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling many of the objects in your application. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML. The configuration metadata allows you to express the objects that compose your application and the rich interdependencies between such objects.

 

container-in-action[1]

1.从app.config中创建对象

IApplicationContext ctx = ContextRegistry.GetContext();

在config中配置

<sectionGroup name="spring">
     <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
     <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
   </sectionGroup>

《context>
      《!-- using embedded assembly configuration file
      《resource uri="assembly://Spring.IocQuickStart.MovieFinder/Spring.IocQuickStart.MovieFinder/AppContext.xml"/>
      -->
      《!-- using section in App.config -->
      《resource uri="config://spring/objects"/>

    《/context>

    《objects xmlns="http://www.springframework.net" >
      《description>An example that demonstrates simple IoC features.《/description>

      《object id="MyMovieLister"
              type="Spring.IocQuickStart.MovieFinder.MovieLister, Spring.IocQuickStart.MovieFinder">
        《property name="MovieFinder" ref="AnotherMovieFinder"/>
      《/object>

《objects>

使用

 

2。 通过using embedded assembly configuration file

同上。

3。 在程序中创建。

GenericApplicationContext ctx = new GenericApplicationContext();
            IObjectDefinitionFactory objectDefinitionFactory = new DefaultObjectDefinitionFactory();
            //Create MovieLister and dependency on
            ObjectDefinitionBuilder builder =
                ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(MovieLister));
            builder.AddPropertyReference("MovieFinder", "AnotherMovieFinder");  

            ctx.RegisterObjectDefinition("MyMovieLister", builder.ObjectDefinition);

            builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(ColonDelimitedMovieFinder));
            builder.AddConstructorArg("movies.txt");
            ctx.RegisterObjectDefinition("AnotherMovieFinder", builder.ObjectDefinition);

            ctx.Refresh();

            return ctx;

4。采用构造函数方式

IApplicationContext context = new XmlApplicationContext("services.xml", "data-access.xml");  located in the bin\Debug

参数还有几种方式:

file:///services.xml"                                                    located in the assembly

"assembly://MyAssembly/MyDataAccess/data-access.xml");

 

还可以用来绑定事件。

 

依赖注入的方式

1。 属性注入。

2。 构造函数注入。

 

Object definition

Table 5.1. Object definition explanation

Property
More info

type
Section 5.2.5, “Instantiating objects”

id and name
Section 5.2.4.1, “Naming objects”

singleton or prototype
Section 5.4, “Object Scopes”

object properties
Section 5.3.1, “Dependency injection”

constructor arguments
Section 5.3.1, “Dependency injection”

autowiring mode
Section 5.3.6, “Autowiring collaborators”

dependency checking mode
Section 5.3.7, “Checking for dependencies”

initialization method
Section 5.6.1, “Lifecycle interfaces”

destruction method
Section 5.6.1, “Lifecycle interfaces”


 

Instantiating objects

1。 构造函数

2。 静态工厂

3。 实例工厂。

 

可以用多种方式配置dependence,event,方法注入,以及Lifecycle interfaces

posted @ 2010-05-26 10:03  Young跑跑  阅读(564)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3