Java Ioc and Java Ioc

Source: Internet
Author: User

Java Ioc and Java Ioc

Author: bamboo

In this chapter, we will talk about Ioc control inversion of Java Spring and DI dependency injection.

Before reading this article, you must thoroughly understand the Java reflection principles.

1. Ioc control Inversion

The original class object instance was created by a programmer through new. After the control inversion, we handed over the object creation function to Spring, and then we can use it directly.

2. DI Injection

Class Bclass

{.......

}

Class Aclass {

Bclass B;

}

For example, when we need to use the Aclass object Aobject, Spring Ioc helps us create an object. At this time, the B attribute in Aobject is empty. Ioc creates another Bclass object, and then uses the set Method to reference the B attribute to the Bclass object created by Ioc.

3. Simulate Ioc implementation

When learning something, if you want to understand it, you 'd better think about how it is implemented inside the framework. If I do it, how can I implement it. Next I will write an Ioc DI program.

 

Spring is equivalent to a Bean factory. You can write your own bean and input the configuration file to automatically generate bean objects.

Next we will create a Java project SimulateIoc to simulate Ioc

 

The dom4j package is required to parse the XML configuration file.

 

Let's write two beans first. The content is the same, but the class name is different.

 

Define a Spring configuration file named IocConfig. xml

 

Next let's take a look at the TestIoc. Java file.

 

Check the running result.

 

Through this file, we can see that we first created a BeanFactory and then obtained the specified Bean object through the getBean method. Is it amazing that we can get what we want through different names. What did BeanFactory do?

 

This is the whole BeanFactory implementation.

Many of them are processing xml. We mainly look at the two marked red points above.

Class bean = Class. forName (cls. getText ());

Object obj = bean. newInstance ();

Class. forName gets the Class of the specified Class. NewInstance () gets the object of the specified class.

Method entryPoint = bean. getMethod (attributename, String. class );

EntryPoint. invoke (obj, value );

GetMethod obtains the method call entry for the specified method. Call invoke to execute the method.

If you want to learn more about the above four sentences. Let's look at the reflection mechanism of the Class file. Or read my other article.

 

The above implementation process of Ioc DI is very simple, and too many Interference Factors and functions are not taken into account. So it is different from Spring implementation, but the principle is the same.

Project Source: http://pan.baidu.com/s/1pKtO5L5

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.