Spring Getting Started Guide--ioc and AOP

Source: Internet
Author: User

Unlike the MyBatis series, when formally starting spring, let's start by understanding the two concepts of spring core, the IoC (inverse of control) inversion and AOP () aspect-oriented programming.

1.IoC (inversion of control) controlled inversion

What is control reversal? Can be so popular to explain, we usually write code when a class is associated with another class will be directly in this class new, for example:

1  Packageday_30_spring;2 3 /**4  * @authorYu Linfung5  *6 * October 30, 20167  */8  Public classpeople {9     PrivateEat Eat;Ten      One     /** A * Create an object instance of eat in the constructor method -      */ -      Publicpeople () { theEat =NewEat (); -     } -      -}

We now "reverse" the control of the object, which is managed by a third-party container, which is managed by the spring container, so that we no longer people the code with the Eat object tightly coupled. Look at the following code:

1  Packageday_30_spring;2 3 /**4  * @authorYu Linfung5  *6 * October 30, 20167  */8  Public classpeople {9     PrivateAction eat;Ten      One     /** A * Create an object instance of eat in the constructor method -      */ -      Publicpeople (Action eat) { the          This. Eat =eat; -     } -      -}

The Eat class is inherited to the action interface, which is in effect a control reversal by means of a dependency injection (di,dependency inversion). By constructing a method to inject. Not dependent on the specific action interface implementation, we only need to pass an action interface class into it. This code actually solved the coupling of the code, compared to the previous "traditional" code, which tightly relies on the action-specific implementation of the Eat class. Spring is constructed in two ways, one of which is the constructor injection We are talking about, and the other is the set method injection . So, the IOC is very simple, the purpose is decoupling, the means is through dependency injection. The spring container helped us solve everything.

2.AOP (Aspect oriented programming) programming for facets

What is the aspect-oriented programming? If you have known about proxy mode, then it is easy to understand AOP, and the principle of AOP is actually implemented by dynamic proxies. Let's take a look at the following example: Before the operation of the database, there will be log records, after the operation of the database will be logged, "usually" we may write.

1  Packageday_30_spring;2 3 /**4  * @authorYu Linfung5  *6 * October 30, 20167  */8  Public classpeople {9     PrivateLogger Logger =NewLogger ();Ten      One      Public voidInsertinti) { ALogger.beforeinsert ();//pre-insert log records -         //Insert a piece of data here -Logger.afterinsert ();//post-insert log records the     } -}

The "single duty" tells us, is this written in the end good or bad? Is logging the people thing to do? Shouldn't it just be responsible for inserting data into the operation? The right thing to do is to have only these lines of code in the People class:

1  Packageday_30_spring;2 3 /**4  * @authorYu Linfung5  *6 * October 30, 20167  */8  Public classpeople {9     Ten      Public voidInsertinti) { One         //Insert a piece of data here A     } -}

So how do we record the log in the Insert method first? We can use dynamic Proxy to implement, that is, to create an object instance of the proxy class, spring has done this for us, just need to configure a few words, can be achieved.

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beans>3     <!--omit other configurations, configure only AOP -4     <Aop:config>5         <Aop:aspectref= "Logger">6             <Aop:pointcutID= "Insert"expression= "Execution (* *.insert (..))"/>7             <aop:beforPoint-ref= "Insert"Method= "BeforeInsert"/>8             <Aop:afterPoint-ref= "Insert"Method= "AfterInsert"/>9         </Aop:aspect>Ten     </Aop:config> One </Beans>

In fact, we can achieve the beginning of our "bad" code function, and more beautiful. Here we just do a simple understanding of the IOC and AOP, in the back will be like MyBatis, first learn to configure from the first step, then learn how to use, and finally to understand the framework source code principle and so on.

Spring Getting Started Guide--ioc and 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.