Beginners Learn spring IOC control inversion and Di dependency transfer summary

Source: Internet
Author: User

30-year-old Cao, 20-year-old body, still in the ability to allow control within the range of 22 o'clock to sleep, 5 o'clock to get up and exercise, to learn, in addition to overtime or late at night, like today, once again wrote the X million people wrote the Spring Ioc summary blog.

First, the IOC control reversal of the detailed:

1). Call dependencies between objects in traditional non-spring management:

We all know that a broken frame of the book says one of spring's core ioc-control inversion, control reversal is the management bean, what is the bean? All Java objects. Where did the Java object come from? A A = new A (), new comes out.

So we define a Pig class:

public class Pig

{

Public String Eat ()

{

Return "Pig will eat!";

}

}

We are defining a class: Person

public class Person

{

Private pig pig;

public void Setpig (pig pig)

{

This.pig Pig;

}

public void Usepig ()

{

System.out.println ("I'm going to feed the pig!");

System.out.println (Pig.eat ());

}

}

Test:

public class Beannstest

{

public static void Mian (string[] args) throws Exception {

Pig pig = new Pig ();

Person person = new persion ();

Person.setpig (pig);

Person.usepig ();

}

}

I'm going to feed the pig!

Pigs will eat!

Described above is the Java traditional way, the caller person actively to create the dependent object pig, and then go to invoke the dependent object pig method eat. For example, the person above actively creates the object pig and then transfers the Pig.eat () output.

2). After using spring, Spring manages the bean by managing the bean between the XML configuration file, such as the human and pig dependencies above:

Bean.xml

<bean id= "Person" class= "Com.test.Person" >

<property name= "pig" ref= "pig" >

</bean>

<bean id= "Pig" class= "Com.test.Pig" >

The Pig in <property> name= "Pig" is the Setpig method in the person method, and the pig of ref= "pig" is the parameter of Setpig;

public class Beantest

{

public static void Mian (string[] args) throws Exception {

ApplicationContext CTX = new Classpathxmlapplicationcontext ("Bean.xml");

Person p = ctx.getbean ("person", Person.class)

P.usepig ();

}

}

Results:

I'm going to feed the pig!

Pigs will eat!

From the second Test result: Using the spring framework, the caller does not have to actively acquire the dependent object, but only passively accepts that the spring container assigns a value to the caller's member variable (config file property), thus, using spring The caller acquires the dependent object in a way that is actively acquired by the original and becomes passively accepted. So Rod Johnson "around Potter." True Mulberry "This approach is called inversion of control IOC.

First, di dependency injection super-detailed:

1). Because the spring container injects the dependent object to the caller (through the configuration file), the caller does not have to actively acquire the dependent object, as long as the spring container is passively waiting to be injected. So Martine Fowler "Horseshoe." Flower "called the dependency injection di.

So spring IOC control inversion is dependency injection di, so spring IOC control inversion is dependency injection di, so spring IOC control inversion is dependency injection di, so spring IOC control inversion is dependency injection di. The important thing to say 4 times.

Of course, spring has been developed to today by the original XML configuration, changed to code inside the annotated way, more convenient point. Subsequent to the free introduction, I hope someone will see in the future.

30-year-old Xiao Cao, 20-year-old body, go ahead!

Beginners Learn spring IOC control inversion and Di dependency transfer summary

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.