I in the last article on the Spring.net "Spring.net Framework Introduction and Module description" in very detailed introduction, this article on the old words from the mention. Let's go straight to the subject.
1. First understand the two interfaces.
Iobjectfactory interface and Iapplicationcontext interface: He has two known as "container" or "IOC container".
The core principle of the spring.net framework is non-intrusive.
The Iobjectfactory interface is the actual container for initializing, configuring, and managing objects.
Iobjectfactory fully qualified named Spring.objects.factory.iobjectfactory,iobjectfactory interface has multiple implementations, where the most
Commonly used is the Spring.Objects.Factory.XML.XmlObjectFactory.
2. Object-defined XML file
Join in App.config or web.config.
<spring>
<context type="Spring.Context.Support.XmlAppliationContext",Spring.Core"/>
<resource uri=file://objects.xml//>
</spring>
Note: The type attribute of the context can be omitted.
The order of the 3.spring and context is limited, and the "Spring/context" has been defined as a string constant in the Spring.net framework to indicate the contact name of the contexts since the Abstractapplicationcontext class. Available by:
Iapplicationcontext Context=contextregistry.getcontext (); To get the contact value, here's a little note. You must make the <spring> contacts in the configuration file work when you use them so that you have to. NET configuration file <configSections> node registers the class Spring.Context.Support.ContextHandler, which lets the Contextregistry class be used to initialize the application context. It implements the IConfigurationSectionHandler interface in the FCL
3.XML object definition can be registered at <objects> node
<section name= "Objects" type= "Spring.context.support.defultsectionhandler,spring.core" >
A simple example of a 4.spring.net IOC container----A simple three-layer implementation
It is useless to say so much, we still have some practical, look at the following simple example:
First, create a project and then add a reference spring.core assembly.
Two. References to System.Configuration----A method that requires the use of ConfigurationManager classes.
Three. Configure the. NET configuration file----specific configuration as follows:
<?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>
<!--这的配置根据实际的程序来的,UsersCompontents是程序集Spring.Demo.Compontext下的一个类-->
<object name="Users"
type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent">
</object>
</objects>
</spring>
</configuration>