IOC of Spring Framework

Source: Internet
Author: User
Preface

To the reader: This article focuses on the IOC (inversion of control) in the spring framework.

I hope that we all put into discussion, let us collide with a more brilliant thought spark. Digression: "I love stupid daughter-in-law" What is IOC

Thinking back when we entered this line, all the experts told us to remember a martial arts secret: It is "high cohesion, low coupling." But when I was young and frivolous, now I looked back, only to find that the essence of the secret.
Well, nonsense not much to say, below to get to the point:
Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs, and is also the core of the lightweight spring framework. Control reversals are generally divided into two types, dependency injection (Dependency injection, short di), and dependent lookup (Dependency lookup). The application of dependency injection is quite extensive.
The above words are very official, it seems that I am also a versatile, however, I am determined to make the fun than to carry forward the end. Below, I give you a story: if there is a, B two people are cooperative relations, a has been using some of the resources of B, this time we can say A to B has a dependency, but one day, a suddenly to use other resources, b No, that B has to expand, when, B will have to change their structure, But how is he going to tell a? A may also have to make some changes, this relationship in our software development, is a strong coupling relationship.
How to improve this situation. We should give the object's construction to a container to concentrate on, so that the coupling between them will no longer exist, it can be said to be a weak coupling relationship. So, the question is, what can we do with spring. Well, the important thing to say three times, Spring helps us manage the components we need, Spring helps us manage the components we need, and spring helps us manage the components we need, which means that mom is no longer worried about my object building.
Anyway, today is Valentine's Day, we programmers are convenient, no object new one out.the specific application

Look at the problem always depends on the essence of good, I am also very pragmatic people not good, then, now, bring you a concrete example:
First of all, it's our Mr. A appearances:

Package Com.weimob.bean;
/**
 * This is a, to invoke B's
 * @author Fulei.yang * */Public
class Usebean {
    private hellobean Hellobean; Public

    void Sethellobean (Hellobean hellobean) {
        This.hellobean = Hellobean;
    }

    public void Show () {
        System.out.println ("displays the message Hellobean. ");
        Hellobean.sayhello ();
    }
}

Next, let's have a round of applause. Mr. B:

Package Com.weimob.bean;
/** *
 Mr. B is here.
 * @author Fulei.yang * * */Public
class Hellobean {
    private String name;
    Public Hellobean (String name) {
        super ();
        this.name = name;
    }
    public void SayHello () {
        System.out.println ("Hello world!");
    }
}

Finally, who told Mr. A to call Mr. B, and how would Mr. B know who called it? Don't worry, Spring has provided us with the configuration file, set up the information bridge between the two, next please: Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd Http://www.springframework.org/sche Ma/context http://www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.springframework . org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--Setting bean properties: scope=" prototype "Setting its Build object mode is not a singleton mode---<bean id=" Usebean "class=" Com.weimob.bean.UseBean "> <property name=" Hellobean "Ref=" Hellobean "></property> </bean> <bean id=" Hellobean "class=" Com.weimob.bean.HelloBean "> </bean> <!--1: Configure Object generation time (lazy-init= "tRue ") 2: Configure initialization and destruction methods-<bean id=" Timebean "class=" Com.weimob.bean.TimeBean "init-method=" Init "destroy-m Ethod= "Destroy" ></bean> </beans>

The above code is very understood, that is, configure the bean, and then use the property tag to determine the dependency, this is called the dependency injection, well, the final appearance is our dear Test class.

        Get configuration file
        applicationcontext ac = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
        Instantiate Usebean
        Usebean bean = (usebean) ac.getbean ("Usebean");
        Call the corresponding method
        bean.show ();

What is the result? As shown below:

So, a simple example of our IOC is done, of course, there are a lot of knowledge about the IOC, such as set method injection and constructor injection, the more difficult point is the injection of the collection, as well as the use of annotations, of course, the importance of annotations in the spring frame is inverse day. Summary

The IOC feature of the spring Framework brings us an efficient way to manage bean components and eliminates coupling between components. is really a good tool for programming, but, again, this is not good framework of the design ideas must be worthy of our study, we might as well go to see the source code, for our progress will be a boon. Space is limited, and we will not dwell on it here.

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.