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.
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 infotype
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