Spring entry (4) [Aspect-Oriented Programming], spring entry-oriented Aspect-Oriented

Source: Internet
Author: User

Spring entry (4) [Aspect-Oriented Programming], spring entry-oriented Aspect-Oriented

Operations such as logs and transactions are often used in the development process. If these operations need to be written in the Business Code, it will be quite troublesome. In this case, we will use Aspect-oriented programming (AOP ), as a programming idea, AOP has different emphasis than OOP. Object-oriented focuses on everything, while object-oriented programming focuses on one aspect of transactions. There are several important concepts in the Aspect-Oriented Programming process: aspect, cut point, connection point, notification,

Notifications: describes the work to be completed. For example, to inject the log function into a method, the log function here is notification. There are five types of notifications: before, After, After-returning, After-throwing, Around

Cut: defines the place where the notification is applied, for example, a method in a class; for example, the print method in the test class;

Cut aspect: the cut-over focus is a modular class consisting of notifications and cut points. In other words, the cut aspect defines all the content to be injected into the target program; specifies the action to be performed on the target method;

Connection point: a point that can be inserted into a plane during program execution, such as method call and member variable initialization. In spring, only method call is supported;


Aspect-Oriented Programming has two implementation methods: Pre-compilation and dynamic proxy. AspectJ is pre-compilation and springAOP is a dynamic proxy mode during runtime. Spring supports AspectJ. The following describes how Spring AOP and AspectJ are used,


Here we use the configuration file method to implement Spring AOP and check the configuration file,

<Aop: config> <! -- Declare a partition class that can have multiple --> <aop: aspect id = "myAspect" ref = "aspect"> <! -- Declare a cut point --> <aop: pointcut id = "point1" expression = "execution (* com.cn. study. day6.Test. * (..)"/> <! -- Notification --> <aop: before method = "before" pointcut-ref = "point1"/> <aop: around method = "around" pointcut-ref = "point1"/> </aop: aspect> </aop: config> <! -- Configure the partition class --> <bean id = "aspect" class = "com.cn. study. day6.Aspect"> </bean>

To configure AOP, the <aop: config> label is used in spring. You can configure multiple surface labels (<aop: aspect>) in this label ), each aspect represents a cross-cutting concern. From the previous illustration, we can know that one aspect contains two parts: a cut point and a notification. in <aop: aspect> defines the vertex (<aop: pointcut>) and notification (aop: before pre-notification) in the tag, and configures the location where the notification is applied in the vertex, that is, any method that matches the Test class (execution (* com.cn. study. day6.Test. *(..))), in the notification label, configure the notification before executing the target method. The switch point is point1. The specific switch class is shown below,

Package com.cn. study. day6; import org. aspectj. lang. ProceedingJoinPoint; // partition class
Public class Aspect {// Method for calling the pre-notification public void before () {System. out. println ("I Am a pre-notification! ");} Public Object around und (ProceedingJoinPoint jp) {System. out. println (" I Am a surround notification! "); Object object = null; try {System. out. println ("before method execution"); object = jp. proceed (); System. out. println ("after method execution");} catch (Throwable e) {// TODO Auto-generated catch block e. printStackTrace ();} return object ;}}

The method in the face-cutting class must be the same as the method name in the method attribute in the notification. The following is the target method,

Package com.cn. study. day6; import org. springframework. cache. annotation. cacheable; import org. springframework. stereotype. component; @ Componentpublic class Test {public void method1 () {System. out. println ("business method! ");}}

The above is a simple business method,
To sum up the meaning of this partition class, that is, before you execute any methods in the com.cn. study. day6.Test class, you will first execute the before method in the com.cn. study. day6.Aspect partition class to view the printed result,

I am a front-end notification! Business method!

The result shows that the before method in the partition class is executed first, and then the method1 method in the target class is executed.

The above is an example of cross-section programming. We implement cross-section programming before the method is called. What is the use of cross-section programming? Let's make a description at the end, and then look at AspectJ



AspectJ is another Implementation of AOP. To use AspectJ, you must enable automatic proxy, as shown below:


This is an automatic proxy for AspectJ, and then you can use AspectJ. See the following Partition class.

Package com.cn. study. day7; import org. aspectj. lang. proceedingJoinPoint; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. aspectj. lang. annotation. pointcut; import org. springframework. stereotype. component; @ Component @ Aspectpublic class AspectTest {// method of calling the pre-notification @ Before ("pointCut1 ()") public void before () {System. out. println ("I Am a pre-notification! ");} // Cut point @ Pointcut (" execution (* com.cn. study. day7.Test. * (..) ") public void pointCut1 (){}}

Because the classes added with @ AspectJ cannot be automatically recognized, the @ Component annotation needs to be loaded. With these two annotations, this class is a partition class, in the partition class, you can declare the partition points and notifications. We define a pointCut1 cut point. The return value of this method must be void. This cut point is for com.cn. study. all the methods in day7.Test are valid. Then, @ Before is used to define a pre-notification and the cut point is pointCut1 (). The result is as follows,

I am a front-end notification! Business method!

The above completes the aspect programming using AspectJ. In addition, after defining the notification, you can also directly use the expression (execution (* com.cn. study. day7.Test. *(..))) instead of referencing a cut point, the advantage of referencing a cut point is that it can be reused after a cut point is defined.

In summary, there are two types of configuration for Implementing AOP. The use scenario of AOP is to add new functions, such as log and transaction control, without disrupting the original code, using AOP can effectively reduce code intrusion and easily implement log, transaction control, and permission control based on the original code. These two methods are widely used in the actual development process. The specific method can be determined based on your own situation.

You are welcome to point out any shortcomings!

Related Article

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.