Brief Analysis of ICO and AOP in Spring framework

Source: Internet
Author: User

Brief Analysis of ICO and AOP in Spring framework

The Chinese name of IoC (Inversion of Control) is "Control Inversion", that is, the application itself is not responsible for the creation and maintenance of dependent objects. The creation and maintenance of dependent objects are the responsibility of external containers. In this way, the control right is transferred from the application to the external container, and the control right is reversed. IoC is also called DI, and dependency injection. The container is responsible for injecting the dependent objects to the upper-layer objects, the upper-layer object only needs to provide an interface declaration. Similar to the inverse in the one-to-multiple relationship of hibernate, the important task of maintaining the relationship is handed over to the other party, and the other party (spring container) is responsible for its own business.

From the perspective of the program, for example, if A wants to call B's method, A depends on B. If A wants to use B, A depends on B.

The so-called inversion, you must understand what will happen if the inversion is not performed, because A must have B before B can be called. If the inversion is not performed, it means that A takes the initiative to obtain B's instance: B B B = new B (). This is the simplest way to obtain B instances (of course, there are also various design patterns that can help you obtain B instances, such as factories, Locator, and so on ), then you can call object B.

Therefore, if you do not put it upside down, it means that A must take the initiative to obtain B before using B. Here, you should understand the meaning of putting it upside down. If A wants to call B, A does not need to take the initiative to obtain B. Instead, other people will automatically send B to the door.

An example of the image is:

Generally, if you are thirsty at home one day and want to drink water, you can go to the commissary of your community and tell them that you need a bottle of water, then the Sales Department will give you a bottle of water!

This is not a big problem. The key is that if the sales department is far away, you must know: how to get to the sales department from your home; whether there is water in the sales department; and whether you need to drive; wait. There may be too many questions to consider. That is to say, for a bottle of water, you may also need to rely on vehicles and other transportation tools. Is the problem complicated? How can this problem be solved?

The solution to this problem is simple: the Sales Department provides the home delivery service. If you are a member of the sales department, you only need to inform the sales department of what you need. The sales department will take the initiative to deliver the goods to you! In this way, you only need to do two things to make it easier:
1. Register as a member with the sales department
Second, tell the Sales Department what you need

Is it similar to Spring? Spring is the sales department, you are the object, and water is the B object
First, declare A class in Spring:
Second: Tell Spring that A requires B

Assume that A is A UserAction class, and B is A UserService class.
<Bean id = "userService" class = "org. leadfar. service. UserService"/>
<Bean id = "documentService" class = "org. leadfar. service. DocumentService"/>
<Bean id = "orgService" class = "org. leadfar. service. OrgService"/>

<Bean id = "userAction" class = "org. leadfar. web. UserAction">
<Property name = "userService" ref = "userService"/>
</Bean>

In the Spring store (factory), there are many objects/services: userService, documentService, orgService, and many Members: userAction. To declare userAction, you need userService, spring will take the initiative to send userService to the door through the channel you provide it, so the sample code of UserAction is similar to the following:

Package org. leadfar. web;
Public class UserAction {
Private UserService userService;
Public String login (){
UserService. valifyUser (xxx );
}
Public void setUserService (UserService userService ){
This. userService = userService;
}
}

In this Code, you do not need to create a UserService object by yourself (Spring acts as the invisible hand behind it, and sends the UserService object to you through the setUserService () method you defined, this is called dependency injection !)

The Implementation Technology of Spring dependency injection is: Dynamic proxy

---------------------------------------------------
Let's talk about AOP, that is, Aspect-oriented programming. The goal of Aspect-Oriented Programming is to separate concerns. What is the focus? What you want to do is focus. If you are a brother-in-law, you have no goals in your life. Every day, you just need to get your foot in your hand. You only know one thing to do all day! So, every day you open your eyes, and you just want to play after dinner (what you have to do), but before you play, you also need to wear clothes, shoes, overlay quilts, cook and so on. These things are your focus, but you only want to eat and then play. What should you do? These tasks can be handled by others. Before you go to the dinner table, there is A special servant A to help you wear clothes, servant B to help you wear shoes, servant C to help you fold the quilt, and servant C to help you cook, then you begin to eat and play (this is your day's business). After you finish your business, you come back, and then a series of servants start to help you do it again, then one day is over!

The advantage of AOP is that you only need to do your business, and others can do other things for you. Maybe one day, if you want to run naked and don't want to wear clothes, you just need to fire servant! Maybe one day you want to bring some money before you go out, then you hire another servant D To Help You Get money! This is AOP. Each person performs their respective duties and flexibly combines them to form a configurable and pluggable program structure.

From the perspective of Spring, the biggest purpose of AOP is to provide the capability of transaction management. Transaction Management is a focus. Your business is to access the database, and you don't want to worry about transactions (too annoying). Therefore, Spring will automatically enable transactions before you access the database, after you access the database, the transaction will be automatically submitted/rolled back for you!

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.