Spring container core mechanism

Source: Internet
Author: User
Tags aop ip number

Spring container core mechanism

I. BACKGROUND

The most central mechanism within spring is the IOC, which controls inversion. It allows you to use an object again without having to go to new, just tell spring the Beanid of the object you want to invoke. This is actually the use of the reflection in Java, Reflection is actually in the runtime to create, invoke the object dynamically, spring is at runtime, according to the configuration file or annotations to dynamically create objects, and invoke the method in the object.

One of the core aspects of spring is AOP, which is the aspect-oriented programming that can be supervised and controlled for a class of objects (that is, calling the module you specify before and after invoking the specific method of such an object) to extend the functionality of a module.

Spring purpose: The relationship between objects and objects (modules and modules) is not associated with code, but is managed through the Configuration class description (Spring uses reflection to dynamically assemble objects within these configurations)

Keep in mind that spring is a container in which the objects in the container will have these services and functions provided by spring.

Second, the principle

(1) Spring IOC principle:

IOC means that the control inversion is the relationship between the container control program, the control to the external container, the previous wording, the program code directly manipulated, and now control by the application code to the external container, the transfer of control is called inversion. There is a very figurative metaphor on the Internet:

How do we find a girlfriend? The common situation is that we go everywhere to see where there is a beautiful body and good mm, and then inquire about their interests, QQ number, telephone number, IP number, IQ number ..., find ways to know them,

Give it what you want, and then hey ... The process is complex and profound, and we have to design and face each link ourselves. The same is true of traditional program development, in which, if you want to use a different object,

You have to get it (your own new one, or a query from Jndi), and then destroy the object (such as connection, etc.) after use, and the object will always be combined with other interfaces or classes.

So how does the IOC do it? It's kind of like finding a girlfriend through a dating agency, introducing a third party between me and my girlfriend: the Marriage Institute. Matchmaking manages a lot of information about men and women, and I can present a list to the matchmaking

Tell it what I want to find a girlfriend, such as like Michelle Reis, figure like Lin Xire, singing like Jay Chou, speed like Carlos, technology like Zidane, and then the matchmaking will be according to our requirements, provide a mm,

We just have to go to love her and get married. As simple as it is, if a matchmaking person doesn't meet our requirements, we'll throw an exception. The whole process is no longer controlled by me, but by a matchmaking-like container.

Institutions to control. This is how spring advocates for development, and all classes are registered in the spring container, telling spring what you are, what you need, and then spring will run to the right time in the system.

Give you what you want, and hand you over to other things that need you. The creation and destruction of all classes are controlled by spring, which means that the object that controls the lifetime of the object is no longer a reference to it.

But spring. For a specific object, it was previously controlled by other objects, and now all objects are controlled by spring, so this is called control inversion.

(2) DI (Dependency injection, Dependency injection)

A key point of the IOC is to dynamically provide the other objects it needs to an object during the system's operation. This is achieved through DI (Dependency injection, Dependency injection).

For example, object A needs to manipulate the database, before we always have to write code in a to get a connection object, with spring, we just need to tell spring,a need a connection, as for this connection how to construct, When constructed, a does not need to know. When the system is running, Spring creates a connection at the right time, and then, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on connection to function properly, and this connection is injected into a by spring, and the name of the dependency injection comes in.

(3) Reflection of class-implementation of dependency injection

What is a class's flyback?

The class description allows you to get the parent class, the implemented interface, the inner class, the constructor, the method, the property, and the constructor to instantiate an object, invoke a method, take the property value, and change the value of the property.

How to get a class description:

Class cls= classes.

Class cls= object. GetClass ();

Class.forName ("Classpath");

How to get a method and evoke it?

Class cls= classes.

Constructor cons=cls.getconstructor (New Class[]{string.class});

Object obj=cons.newinstance (New object[]{"AAA"});

Method Method=cls.getmethod ("Methods name", New Class[]{string.class,integer.class});

Method.invoke (obj,new object[]{"AA", New Integer (1)});

Third, the use of

All the projects that you don't want to manage the build and create for your objects can be used with spring.

Iv. Conclusion

In this paper, the core mechanism of the spring container, the IOC, to understand how spring is used. Spring all the components of the implementation of basic is inseparable from its core mechanism, AOP this time did not speak, there will be another specifically to SPRINGAOP.

If you want to understand the springioc of the lower level of implementation, you can refer to the article "Spring Framework design concept and Design pattern analysis", link https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/

Spring container core mechanism

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.