Spring. Net Applications in non-web environments and spring containers.

Source: Internet
Author: User

In another article, spring. NET is used in the web.Article. This is a non-web application. Relatively simple.

We Introduction Spring. Core , Use Spring. net To implement the factory Model . The process is as follows:

 

A)AddApp. configApplicationProgramConfiguration file.

B) In App. config Medium Registration Spring Node, add the configurationCode(Note: It is inserted between <configuration> and </configuration> .) :

 

Code

<! --  
SP1: You must register this class in the <configsections> node of the. NET configuration file,
After the node processor is registered, the <spring> node in the configuration file can be started.
Function. (Configsections must be the first element in configuration.
Otherwise, an error occurs during compilation .)
Context: List of container resources (no small number of resources, errors may occur if fewer resources are used)
Objects: list of objects in the container (no fewer objects, errors will occur if fewer objects are used)
-->
< Configsections >
< Sectiongroup Name = "Spring" >
<! -- Webcontexthandler is used in web projects -->
<! -- Section name = "context" type = "Spring. Context. Support. webcontexthandler, spring. Web "/ -->
<! -- Contexthandler is used in projects other than the Web -->
< Section Name = "Context" Type = "Spring. Context. Support. contexthandler, spring. Core" />
< Section Name = "Objects" Type = "Spring. Context. Support. defaultsectionhandler, spring. Core" />
</ Sectiongroup >
</ Configsections >

 

 

C) In App. config In the application configuration file, add Context And Objects Configuration code of the object (Note: You can add the following code after the configuration above ):

 

Code

<! --
SP2: configure the spring container so that the configuration does not need to be explicitly created in the program.
Spring container, thus reducing the coupling of the program to spring.
<Context> the type attribute of a node is optional. In Windows applications, the default value is spring. Context. Support. xmlapplicationcontext.
-->
< Spring >
< Context >
<! -- SP3: the configuration file here refers to the XML file that includes the spring. Net object definition, not the. config file. -->
< Resource Uri = "Config: // spring/objects" />
<! -- The URI syntax for referencing embedded resources in. Net assembly is as follows:
Assembly: // <assemblyname>/<namespace>/<resourcename>
Assembly: // <Assembly>/<namespace>/<Resource Name>
Sp_manual: add different xml configuration information for different projects. For example:
Example: <resource uri = "assembly: // piggy. net. Web/piggy. net. Web/webtest. xml"/>
-->
< Resource Uri = "Assembly: // piggywinform/piggywinformtemplet/objects. xml" />
</ Context >
<! -- SP4: The xmlns element of the objects node is required. Different namespaces must be added based on different applications. -->
< Objects Xmlns = "Http://www.springframework.net" />
</ Spring >

 

D) In Piggywinform Add programs Objects. xml The Code is as follows (this file is configured to inform Spring Container, which object is used and which class in the Assembly ):

 

Code

<? XML version = "1.0" encoding = "UTF-8" ?>

objects xmlns =" http://www.springframework.net "
xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "
xsi: schemalocation =" http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd " >

<ObjectID= "Springtest"Type= "Piggywinformtemplet. springtest, piggywinform" />

</Objects>

 

The configuration has been completed. Next, I paste the spring container class that I have encapsulated to facilitate future use. The Code is as follows:

 

Code

Using Spring. context;
Using Spring. Context. Support;

namespace piggy. common
{< br> Public class springtool
{< br> private static iapplicationcontext m_context = null ;

PrivateSpringtool ()
{
}

Public   Static   Void Close ()
{
If (M_context ! =   Null )
{
Abstractapplicationcontext C = (M_context As Abstractapplicationcontext );
If (C ! =   Null )
C. Dispose ();
M_context =   Null ;
}
}

Public   Static Iapplicationcontext Context
{
Get
{
If (M_context =   Null )
{
Lock ( Typeof (Springtool ))
M_context = Contextregistry. getcontext ();
}
Return M_context;
}
}
}
}

 

In this way, we can easily use it elsewhere, such as object myobject = springtool. Context. GetObject ("springtest"). Pay attention to the string parameter here. springtest isObjects. xmlTheObjectID.

Source code download

 

Original Works from the effort to be lazy, reprint please explain the source of the article: http://www.cnblogs.com/kfarvid/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.