dependency injection

This article is about the DI of spring.net.

The underlying statement demonstrates how the spring.net works.

First:Config your file App.config as follows.

View Code
<?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>
<object name="MyDataAccess" type="DataAccessComponent.DefaultDataAccess,DataAccessComponent" />
<object name="jialefuDataAccess" type="DataAccessComponent.jialefu,DataAccessComponent" />
</objects>
</spring>
</configuration>

Please pay attention on the node of <object />.It contains the information of name,class,assembly.It is the reason why you

can activate specific instance(that is [assembly]class) according to the name.

Second.

You can create your arbitrary instance in your program.

View Code
IApplicationContext context = ConfigurationManager.GetSection("spring/context") as IApplicationContext;

IDataAccess dac
= context.GetObject("MyDataAccess") as IDataAccess;
var r1
= dac.ExecuteNonQuery("");

IDataAccess da
= context.GetObject("jialefuDataAccess") as IDataAccess;
var r
= da.ExecuteNonQuery("");

posted @ 2011-06-28 08:50  cnbwang  阅读(119)  评论(0编辑  收藏  举报