The Ioc&di of the 3.Spring series

Source: Internet
Author: User
Tags java se

I. What is the IOC?

1. Concept

Ioc-inversion of control, or "inversion of controls", is not a new technique, but a design idea. In Java development, IOC means handing over the objects you have designed to the container control, rather than the traditional direct control within your object;

2. Who controls who, what controls

Traditional Java SE Programming, we create objects directly within the object through new, is the program to create dependent objects, and the IOC has a special container to create these objects, that is, the IOC container to control the creation of objects; Note: The IOC container controls the object The main control is external resource acquisition (not just objects such as files, etc.).

3. Why is it reversed and what aspects are reversed

There is a reversal of the forward, the traditional application is our own active control in the object to directly obtain the dependent object, that is, the reverse is the container to help create and inject dependent objects;

Why reverse: Because the container helps us to find and inject dependent objects, the object is only passively accepting dependent objects, so it is reversed;

What's reversed: the acquisition of the dependent object is reversed.

4. Summary:

To put it bluntly, the object does not need you to go to new, but instead gives it to the IOC to help you create the object, and helps us to establish a relationship between the classes and the class, which we need to retrieve.

Second, what is di?

In fact, Di and IOC are the same concept, because in the Applicationcontext.xml configuration file, the bean and bean are dependent on each other when they are maintained by ref, so they are called dependency injection. That is, control reversal.

Third, examples

We deepen the understanding of spring Ioc&di through an example:

1. Traditional Java programs, where two entities relate to each other:

 Public classa{ Public voidShow () {System.out.println ("A Show () ..."); }} Public classb{PrivateA;  Public voidShow (A A) {a.show (); }} Public classmain{ Public Static voidMain (string[] args) {A a=NewA (); b b=NewB ();   B.show (a); }}

As we can see from the code above, if we use a combination, then it is necessarily cumbersome to use, not only to create a B object, but also to create a object and then pass the A object to the B method before calling the Show method.

However, after using spring, you only need to configure two associations in the IOC container, in the test method, direct b b = new B (), and A is automatically created and contained in B, without the need for us to create a object, save a lot of operations, we take a look at the creation of the IOC container object, What the IOC container does for us:

First, create two beans, namely parent and child:

 public  class   Parent { private          child child;  public  void   SetChild (Child child) { this . Child = child;   public   child Getchild () { return   child;  public   Parent () {System.out.println (    span> "Parent Constructor ..." 
 Public class Child {    public child  () {        System.out.println ("Child Constructor ...");}    }

Next, configure the above two beans and the reference relationship in Applicationcontext.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >    <BeanID= "Child"class= "Com.spring.model.Child"></Bean>        <BeanID= "Parent"class= "Com.spring.model.Parent">        < Propertyname= "Child"ref= "Child"></ Property>    </Bean></Beans>

Finally, the test program:

 Public class Main {    publicstaticvoid  main (string[] args) {        new Classpathxmlapplicationcontext ("Applicationcontext.xml");}    }

After execution, output:

Child Constructor ... Parent Constructor ...

Note: As long as the bean is configured in an IOC container, the IOC container creates an instance of the bean through the parameterless constructor in a reflected way through the fully qualified name of the bean configured inside it.

We get the bean in the test program because the correlation is already configured within the IOC container:

 Public class Main {    publicstaticvoid  main (string[] args) {        new Classpathxmlapplicationcontext ("Applicationcontext.xml");         = Ctx.getbean (Parent.  Class);        System.out.println ("parent=" + parent);        System.out.println ("child=" + Parent.getchild ());}    }

After execution, the output results:

Child Constructor ... Parent constructor...parent=[email protected]child[email protected]

Iv. Summary

The bean instantiation and the bean-to-bean dependency are given to the IOC container to instantiate, which we need to retrieve.

PS: Light to see the concept may not be easy to understand, self-action, will be more clear!

The Ioc&di of the 3.Spring series

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.