Spring.net Dependency Injection Framework Learning--Simple Object injection
The concepts of dependency injection and the core modules of the Spring.net framework are explained in the previous two articles, and today we'll look at how to use Spring.net to implement a simple object injection
Common Files
The following files are frequently used in our use of the Spring.net framework:
- Common.Logging.dll includes features for spring.net logging (required)
- Spring.Core.dll contains the core library of the spring.net (required)
- Spring.Data.dll contains the Spring.net data access function
- SPRING.AOP. dll contains spring.net support for facet-oriented programming (AOP)
- The Spring.Web.dll contains a series of functional extensions to ASP. spring.net
Spring.net Common Interface
Iobjectfactory and Iapplicationcontext
The Iobjectfactory interface provides an advanced configuration mechanism to manage any type of object, providing a configuration framework and basic functionality.
Iapplicationcontext is a iobjectfactory sub-interface that represents the spring IOC container, which is responsible for instantiating, configuring, and assembling many objects in the application. The container obtains the instantiation, configuration, and assembly of objects by reading the configuration metadata. The configuration metadata is represented in XML. It adds more enterprise-specific functionality with spring.net aspect-oriented programming (AOP) capabilities, message resource processing (for internationalization), event propagation, and easier integration of Web application contexts (such as Web application contexts) for use in Web applications Iapplicationcontext 。 Iapplicationcontext is a complete superset of the iobjectfactory.
Spring.net provides several implementations of the Iapplicationcontext interface. In a standalone application, an instance of Xmlapplicationcontext is typically created programmatically or declaratively in an application's app. config file.
In a Web application, Spring provides a webapplicationcontext implementation by adding a custom HTTP module and an HTTP handler in the Web. config file to configure
It's spring.net's working principle diagram.
As shown, the Spring.net IOC container uses one form of configuration metadata; This configuration metadata represents how the application developer tells the Spring container to instantiate, configure, and assemble objects in the application. Configuration metadata is provided in a simple and intuitive XML format
spring.net
Introduction to Configuration
Spring.net configuration files (typically added to app. config or Web. config) are the following XML file formats
the node that must be in the configuration file
Spring node
Spring node Mode one
<spring> <context type="Spring.Context.Support.XmlApplicationContext, Spring.core "> <resource uri="file://objects.xml"/> <resource uri= " Assembly://myassembly/myproject/objects-dal-layer.xml "/> </context></spring>
Spring node mode two
<spring> <context> <resource uri="file://objects.xml"/> <resource uri="assembly://myassembly/myproject/objects-dal-layer.xml"/>
The Type property of the <context> node is optional, and in the Windows app, its default value is Spring.Context.Support.XmlApplicationContext, so mode one and mode two are identical
The names of spring and context nodes are not arbitrary and must be "spring" and "context", and spring.net itself will "Spring/context" As a string constant defined in the Abstractapplicationcontext class to represent the node name of the context so this node name can not be arbitrarily changed, to refer to the container created by the above configuration, you can use the following code:
Iapplicationcontext CTX = Contextregistry.getcontext ();
The Contextregistry class can be used either to initialize the application context or to access objects in the container in a service locator style, noting that What makes this possible is the Spring.Context.Support.ContextHandler class, which implements the IConfigurationSectionHandler interface of the FCL. Must be in. NET configuration file in the <configSections> node to register the node processor, the <spring> node in the configuration file will function as follows:
configsections Knot Point
<configSections> <sectiongroup name="spring"> <section name= " Context " type="Spring.Context.Support.ContextHandler, Spring.core"/>
Source data Object XML file format
<objects xmlns="http://www.springframework.net"> <ObjectId="..."Type="..."> <!--collaborators and configuration for This ObjectGo here---</Object> <ObjectId="...."Type="..."> <!--collaborators and configuration for This ObjectGo here---</Object> <!--moreObjectDefinitions go here--></objects>
The id attribute is a string used to identify a single object definition. The Type property defines the types of the object and uses the fully qualified type name, including the assembly name. The value of the id attribute refers to the collaboration object.
Example Introduction
Person.cs
/***************************************************** * ProjectName:Spring.NET01 * Description: * Classname:perso n * clrversion:4.0.30319.42000 * AUTHOR:JIYF * NameSpace:Spring.NET01 * machinename:jiyf_pc * createtime: 2018/1/14 11:59:12 * UPDATEDTIME:2018/1/14 11:59:12*****************************************************/usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacespring.net01{ Public classPerson { PublicPerson () {}~Person () {} Public voidprint () {Console.WriteLine ("I am a Person object"); } }}
We configure a person object according to the format of the profile
App. Config file
<?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> <!--the source of metadata objects <resource uri="config://spring/objects"></resource> </context> <objects xmlns="http://www.springframework.net"> <!--a Person object--<ObjectId="Person1"Type="spring.net01.person,spring.net01"> </Object> </objects> </spring></configuration>
Injecting an object through a sprint.net container
The first step refers to the Spring.net dynamic library file, where you only need to reference Spring.Core.dll and Common.Logging.dll dynamic library files to
Code test:
classProgram {Static voidMain (string[] args) { //Normal object CreationPerson person =NewPerson (); Person.print (); //injecting objects through the spring.net IOCIapplicationcontext CTX =Contextregistry.getcontext (); Person Bennanhai= (person) ctx. GetObject ("Person1"); Bennanhai.print (); Console.read (); } }
Execution Result:
Such a simple dependency injection example implements the
Source Code Engineering Download
Spring.net Dependency Injection Framework Learning--Simple Object injection