SpringNet learning Notes 1. SpringNet learning notes

Source: Internet
Author: User

SpringNet learning Notes 1. SpringNet learning notes

--- Restore content start ---

I recently read the springnet framework knowledge shared by the great gods in the garden. I feel pretty good. I also studied springnet in my spare time. I have read the basic IOC and AOP articles of springnet containers over the past few days. I also want to share some of my understanding with you and record my study notes. You are welcome to raise a question to solve it.

Springnet first describes what control inversion is. FirstInversion of Control (IoC), Also calledDependency Injection). I personally understand springnet's control reversal and it's like a complicated abstract factory. The inversion of control refers to converting the process of creating an instance by a developer into container injection to load the instance, and finally achieving loose coupling. This is similar to using an abstract factory to reflect the dynamic loading of an assembly to create an instance. In my personal understanding, I hope to correct the shortcomings. Describes the purpose of control reversal. The following describes the operation.

The following describes how to implement springnet dependency injection:

It can be roughly divided into five points. If there are deficiencies, we hope to correct them. Below we will write some code to demonstrate my dependency injection implementation and the differences with the abstract factory:

First, create an API IPerson. Only the Person method signature is provided in the API.

namespace Myspringdemo{    public interface IPerson    {        void Person();    }}

Create a NewPreson class to inherit the methods in the IPerson implementation interface.

Namespace Myspringdemo {public class NewPerson: IPerson {public void Person () {Console. WriteLine ("I Am a factory maker ");}}}View Code

 

 

To compare with the abstract factory, I created another factory class to create an instance.

Namespace Myspringdemo {public class GetFactory {public static IPerson person; public static IPerson Instance () {person = CallContext. getData ("Person") as IPerson; if (person = null) {person = new NewPerson (); CallContext. setData ("Person", person);} return person ;}}}GetFactory

Then we can call it in the program class.

Namespace Myspringdemo {public class Program {static void Main (string [] args) {IPerson Factoryper = GetFactory. Instance (); Factoryper. Person (); // factory implementation method }}}

The result is as follows:

The above is the abstract factory implementation. The following describes the implementation of dependency injection.

1. Reference Assembly: Common. Logging. dll and spring. core. dll

2. Configure the resolution Node

<! -- Configure the spring node resolution type --> <! -- The configSections node must be followed by the configuration node --> <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>

3. Configure the spring node and objects node, and configure the simple dependency injection for the object node.

<! -- Configure the spring.net node --> <spring> <context> <resource uri = "config: // spring/objects "/> </context> <objects xmlns =" http://www.springframework.net "> <description> description </description> <! -- Place the node location in the container --> <! -- Type is the assembly to be reflected, written as the name of the current namespace. class name, namespace name --> <object name = "Myspring" type = "Myspringdemo. springPerson, Myspringdemo "> </object> </objects> </spring>

After configuring the OK node, the following is called: Create a springperson class to inherit IPerson and implement the method in the interface

Namespace Myspringdemo {public class SpringPerson: IPerson {public void Person () {Console. WriteLine ("I am from spring ");;}}}

Method for implementing dependency Injection

Namespace Myspringdemo {public class Program {static void Main (string [] args) {IApplicationContext ctx = ContextRegistry. getContext (); // create the context IPerson per = ctx. getObject ("Myspring") as IPerson; per. person (); // call IPerson Factoryper = GetFactory. instance (); Factoryper. person (); // factory implementation method }}}

Let's print the result.

After successfully printing the result of dependency injection, the feeling of getting started with dependency injection is not much different from that of the factory. After several days, we found that the method of dependency injection is indeed better than the maintainability and scalability of the factory, the efficiency may be better. I didn't know much about it. Good friends can correct it and talk about different understandings!

--- Restore content end ---

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.