Chapter 3 prepare and build the spring. Net Environment

Source: Internet
Author: User

In the previous chapter, we introduced the concept of dependency injection and inversion control. Next, let's build the spring. NET environment and see what control inversion and dependency injection look like in spring. net.

3.1 spring. netDownload

On spring. Net official website http://www.springframework.net/we can download all the information about spring. net, including the installation files, documentation andCode. We often use the following files when using the spring. NET Framework:

Common. Logging. dll includes spring. Net log functions (required)

Spring. Core. dll contains the core library of spring. Net (required)

Spring. Data. dll contains the spring. NET data access function.

Spring. AOP. dll includes spring. Net's support for Aspect-oriented programming (AOP ).

Spring. Web. dll includes spring. NET and provides a series of function extensions for ASP. NET.

3.2Spring. netConfiguration

After downloading the relevant spring. Net files, some simple configurations are required to use the spring. NET Framework.

Spring. Net configuration file (usually added to app. config or web. config ):

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Configuration  >  3     <  Configsections  >  4       <  Sectiongroup Name  = "Spring"  >  5         <! --  Provide spring for ApplicationsProgramContext support  -->  6         <  Section  Name  = "Context"  Type  = "Spring. Context. Support. contexthandler, spring. Core"  />  7        <! --  Provides spring support for object containers  -->  8         <  Section  Name  = "Objects"  Type  = "Spring. Context. Support. defaultsectionhandler, spring. Core"  />  9       </  Sectiongroup  >  10    </  Configsections  >  11     <  Spring  >  12       <  Context  >  13         <! --  Definition of the object XML file used by the IOC container in spring  -->  14         < Resource  Uri  = "Assembly: // cnbloglesson_3_2/cnbloglesson_3_2.config/objects. xml"  />  15       </  Context  >  16     </  Spring  >  17   </  Configuration  > 

Configuration file of spring. Net object:

1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Objects  Xmlns  = "Http://www.springframework.net"  >  3     <  Object  ID  = "Readerdal"  Type = "Cnbloglesson_3_2.readerdal, cnbloglesson_3_2"  >  4     </  Object  >  5   </  Objects  > 

In an XML-based object, <Object> is a node, and the parent node of all nodes must be <objects>, xmlns is required in the properties of <objects xmlns = "http://www.springframework.net">. And the XMLSet as embedded Resource, Right-click, choose Properties> Generate, and set the operation to embedded resources. Otherwise, spring. Net Cannot initialize the object.

3.3 start constructionSpring. netEnvironment

First, we are still preparing spring first. net files. Here I am currently using spring. net-1.3.2 (because the file has more than 50 MB, you can download it from various websites), open Visual Studio, and create a console project. Because I am currently using. netframework 4.0, I will find spring.net-1.3.2.zip \ spring. Net \ bin \ net \ 4.0 \ release and reference the required files to the project. Here, I add all commonly used files to the Project for convenience and add references to these files.

 

Next, we need to add spring. net configuration file and spring.. Net object configuration file (for example, config/objects. XML), because it is a console project, add the app here. config as the configuration file. Section 3.2 describes how to configure the spring. Net configuration file and spring. Net object.

Next, we will add a code file. For ease of understanding, the example is still: Chapter 2 "getting a blogArticle. Except for readerservice. CS, the code is implemented using spring. Net dependency injection, and no other classes are changed.

 1   Using  System;  2   Using  Spring. context; 3   Using  Spring. Context. Support;  4   Using  Spring. Core. IO;  5   Using  Spring. Objects. Factory. xml;  6   Using  Spring. Objects. factory;  7   8   Namespace  Cnbloglesson_3_3 9   {  10       ///   <Summary>  11       ///  Business logic for reading articles  12       ///   </Summary>  13       Public   Class  Readerservice: ireaderservice  14   { 15           ///   <Summary>  16           ///  Data Reading objects  17           ///   </Summary>  18           Private  Ireaderdal;  19   20           Public  Readerservice ()  21  {  22               /*  Either xmlobjectfactory or iapplicationcontext can be configured.  */  23   24               //  Or use xmlobjectfactory to configure  25 Iresource input = New Filesystemresource ( "  File: // C:/objects. xml  "  ); 26 Iobjectfactory factory = New  Xmlobjectfactory (input );  27 Dal = (ireaderdal) Factory. GetObject ( "  Readerdal  "  );  28   29               //  Use iapplicationcontext to configure  30 Iapplicationcontext context = Contextregistry. getcontext (); 31 Dal = (ireaderdal) Context. GetObject ( "  Readerdal  "  );  32   }  33   34           ///   <Summary>  35           ///  Reader name  36           ///  </Summary>  37           Private   String Name { Get ; Set  ;}  38   39           ///   <Summary>  40           ///  How to read an article  41           ///  </Summary>  42           Public   Void  Getarticle ()  43   {  44   Dal. getarticle ();  45   }  46   }  47 }

Add the config/objects. xml file

Objects usually collaborate with each other. We can also say that they are dependent on each other. Spring. NET provides an iobjectfactory interface. We use the iobjectfactory interface to initialize the. configure and manage the object container (which is the parent interface of all containers ).

There are three important exclusive terms which will be explained in detail below: object, object factory, application context.

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?>  2   <  Objects  Xmlns  = "Http://www.springframework.net"  Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"   > 3   4     <  Object  ID  = "Readerdal"  Type  = "Cnbloglesson_3_3.readerdal, cnbloglesson_3_3"  >  5       6     </  Object  >  7   8   </ Objects  > 

 

The object is the configured object in the configuration file. The previously mentioned object is the readerdal configured above.

The iobjectfactory interface provides an advanced configuration mechanism for spring. Net to manage and maintain objects.

The iapplicationcontext interface extends the iobjectfactory and adds the Aspect-Oriented Programming and message resource processing functions.

Spring. Objects. Factory. iobjectfactory has multiple implementation classes, the most commonly used is

Spring. Objects. Factory. xml. xmlobjectfactory. With xmlobjectfactory, we can define, assemble, and publish service objects by configuring XML files.

To put it simply, the iobjectfactory interface provides the configuration framework and basic functions, and the iapplicationcontext interface also expands many enterprise-level features. It can be said that iapplicationcontext is a superset of iobjectfactory and has all the functions and behaviors of iobjectfactory.

The above statements may be too conceptual. Next, let's take a look at the example in cnbloglesson_3_3. We use spring. Objects. Factory. xml. xmlobjectfactory to manage objects:

1 //Or use xmlobjectfactory to configure2Iresource input =NewFilesystemresource ("File: // C:/objects. xml");3Iobjectfactory factory =NewXmlobjectfactory (input );4Dal = (ireaderdal) Factory. GetObject ("Readerdal");

The iresource interface is very important. We will discuss it with you later. Now we only need to know that it is an interface for reading resources.

The so-called,

Object: the instance of the cnbloglesson_3_3.readerdal class in the example.

Object Factory: iobjectfactory object example. Because of New xmlobjectfactory (input);, an object factory instance is obtained.

 
1 //Use iapplicationcontext to configure2Iapplicationcontext context =Contextregistry. getcontext ();3Dal = (ireaderdal) Context. GetObject ("Readerdal");

Application context: contextregistry. getcontext (); the application context is created.

Through the previous example, we can see that through spring.. Net reduces the coupling between the business logic layer and the data access layer, but in fact it does not completely solve the coupling, but only places the coupling to the object. XML file. A dependency is formed when a container is running, that is, the interface implementation is dynamically injected into the class. I usually regard this IOC mode as an upgraded factory mode, and use spring. net IOC container is regarded as a large factory, but the objects generated by this factory are defined in the external XML file.

Sample Code: http://download.csdn.net/detail/cilence6788/5079971

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.