Spring's control inversion (IoC), Dependency Injection (DI), and aspect-oriented (AOP)

Source: Internet
Author: User
Tags reflection

About Dependency Injection, this blog post is very simple and easy to read.

Https://github.com/android-cn/blog/tree/master/java/dependency-injection

In addition, the three common DI implementations of IOC are described in blog http://blog.csdn.net/hy6688_/article/details/40658835. -constructor injection, setter injection, interface injection.

Dependency Injection (DI)--(Dependency-injection)

1. Dependency If there is an instance of Class B in Class A, then class A is said to have a dependency on class B. For example, the following class Human uses a Father object, and we say that class Human has a dependency on the class Father.

Human {    ...    Father Father;    Father ();}}  

Looking closely at this code, we will find that there are some problems:
(1). If you want to change the Father generation mode now, if you need to new Father(String name) initialize the father, you need to modify the Human code;
(2). If it is difficult to test the effect of different Father objects on Human, the initialization of Father is written dead in the Human constructor;
(3). If the new Father() process is very slow, it is also difficult to Mock off the process with an already initialized Father object in a single measurement.

2. Dependency injection the reliance on direct initialization in the constructor is a hard init method, with the disadvantage that two classes are not independent enough to test easily. We also have another Init method, as follows:

Human {    ...    Father Father;    Human (this= Father;}}    

In the above code, we pass in the Father object as a parameter to the constructor. The Father object has been initialized externally before calling Human's constructor method. Like this kind of non-self-initiated dependency, and through the external to pass through the dependency, we are called dependency injection.
Now we find that the two problems in 1 above are well solved, and simply say that dependency injection has two benefits:
(1). Decoupling, decoupling the dependencies.
(2). Because it is decoupled, it is convenient to do unit tests, especially Mock tests.

3. Dependency Injection in Java

There are several ways to implement dependency injection, and in Java, annotations are most commonly used. Automatic injection of dependent objects is achieved by tagging @Inject annotations before the declaration of the field.

Human {    ...    Human () {}} 

The above code looks amazing: Just add an annotation and the Father object will be automatically injected? How is this injection process done?

Essentially, if you just write a @Inject note, Father is not automatically injected. You also need to use a dependency injection framework and make a simple configuration. Now the Java language of the more popular dependency injection framework has Google Guice, Spring, and more popular on Android have Roboguice, Dagger, etc.

inversion of Control (IoC)-- (inversion of Control)

IoC, see also: Http://blog.163.com/[email protected]/blog/static/50639037200721345218382/

IoC (Inversion of Control), this is the core of spring, throughout. The so-called IOC, for the spring framework,is that spring is responsible for controlling the object's life cycle and the relationship between objects.。 What does that mean, to give a simple example that we are
How to 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, phone number, mobile phone number, MSN ..., find ways to know them, give it what they want, then hey ... This process is complex
Esoteric, we must design and face each link ourselves. This is also true of traditional program development, in which, if you want to use a different object, you must create it (either yourself, or a query from Jndi), and then destroy the object after use (for example,
, connection, etc.), objects 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. A third-party institution was introduced between me and my girlfriend: the marriage agency. The matchmaking manager has a lot of information about men and women, and I can give a list to the dating agency and tell it what kind of woman I'm looking for.
Friends, such as like Michelle Reis, figure like Lin Chi-ling, singing like Andy Lau, the speed like Liu Xiang, tall like Yao Ming and so on, and then the matchmaking will be in accordance with our requirements, provide a mm, we just have to go and her love, married on the line. Simple and clear, if the matchmaking gives me
Our candidates do not meet the requirements, we will throw an exception. The whole process is no longer controlled by myself, but by a similar container-like institution that has a matchmaking system. 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 spring will give you what you want when the system runs to the right time, and also give you what you need. The creation and destruction of all classes are controlled by spring, which means that the control object survives
The cycle is no longer the object that references 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。 If you do not understand, I decided to give up.A key point of the IOC is to dynamically provide the other objects it needs to an object while the system is running .this is achieved through DI (Dependency injection, Dependency injection) .。 For example, object A needs to manipulate the database, and we've always had to
Have written code 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 constructs, a does not need to know. When the system is running, spring will be in the appropriate
Time to make a connection, and then, like an injection, inject 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 by spring into a, the name of the dependency injection
That's how the word came. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows the program to dynamically generate objects, execute object methods, and change object properties when it is run, and spring is implemented by reflection.
Injected.


Facet-oriented (AOP)--(Aspect oriented
Programming)
AOP Reference: https://www.zhihu.com/question/24863332/answer/48376158
Aspect-oriented programming (AOP is the acronym for Aspect Oriented program), we know that object-oriented features are inheritance, polymorphism, and encapsulation. Encapsulation requires that functions be dispersed into different objects, which is often referred to as assignment of responsibilities in software design. In fact, let's say that different classes are designed to have different methods. So the code is scattered in a class. The benefit of this is to reduce the complexity of the code and make the class reusable.
However, it is also found that the code duplication is increased while the code is dispersed. What do you mean? For example, in two classes, we may need to log in each method. In terms of object-oriented design, we have to include the contents of the log in the methods of two classes. Perhaps they are exactly the same, but it is because object-oriented design makes it impossible for a class to be associated with a class, but not to unify the duplicated code.
Someone might say, well, we can write this code in a separate class-independent method, and then call it in these two classes. However, in this way, these two classes are coupled with the independent classes we mentioned above, and the changes will affect these two classes. So, is there any way that we can randomly add code when we need it? at run time, the programming idea of dynamically cutting code into the class, at the specified location, is the aspect-oriented programming.
In general, the code snippet in which we cut into the specified class is called a slice, and which class to cut into and which method is called the pointcut. With AOP, we can extract several classes of common code into a slice, and then cut into the object when needed, thus altering its original behavior.
In this view, AOP is only a complement to OOP (object Oriented Programming OO programming). OOP distinguishes a class from a landscape, while AOP adds a specific code to the object vertically. With the Aop,oop became three-dimensional. If you add a time dimension, AOP makes OOP from the original two dimensions into three-dimensional, from the plane to three-dimensional. Technically, AOP is basically implemented through proxy mechanisms.
AOP can be a milestone in programming history, and it is a useful complement to OOP programming.

Spring's control inversion (IoC), Dependency Injection (DI), and aspect-oriented (AOP)

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.