Advanced Spring AOP -- source code implementation (2) Advisor and Aspect in Spring AOP, aopadvisor

Source: Internet
Author: User

Advanced Spring AOP -- source code implementation (2) Advisor and Aspect in Spring AOP, aopadvisor

The example of the complete source code address: https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9% AB %98%E7%BA%A7%E2%80%94%E2%80%94%E6%BA%90%E7%A0%81%E5% AE %9E%E7%8E%B0%EF%BC%882%EF%BC%89Spring%20AOP%E4%B8%AD%E9%80%9A%E7%9F%A5%E5%99%A8%EF%BC%88Advisor%EF%BC%89%E4%B8%8E%E5%88%87%E9%9D%A2%EF%BC%88Aspect%EF%BC%89

 

The reason why I haven't officially entered the source code of Spring AOP is that I encountered a little trouble when I was reading Spring AOP to generate proxy objects, so I had to temporarily stop it. Instead, I had to clarify the two conceptual issues related to Spring AOP.

I have not mentioned the concept of "notifier" in my previous blog. In Spring practice, I simply explained that <aop: advisor> is used to define a notifier in xml, later, we didn't explain it, but used <aop: aspect> to define a plane. In Spring technology insider, the Spring AOP chapter introduces three concepts of AOP: notification, cut point, and notification device. At this moment, I had a lot of doubts about the "Notification device". After reading the relevant information, I was not satisfied with the answer, so I decided to check it out.

First, we will discuss how to use the notification tool. Define a notification class, which includes both pre-notification and post-notification. Note that if you use <aop: advisor> to define the method for Implementing AOP, You need to implement the Advice interface in the notification class, the pre-notification method corresponds to MethodBeforeAdvice, and the post-notification method corresponds to AfterReturningAdvice.

1 package com. demo; 2 3 import org. springframework. aop. afterReturningAdvice; 4 import org. springframework. aop. methodBeforeAdvice; 5 import org. springframework. stereotype. component; 6 7 import java. lang. reflect. method; 8 9/** 10 * Created by Kevin on. 11 */12 @ Component ("advisorTest") 13 public class AdvisorTest implements MethodBeforeAdvice, afterReturningAdvice {14 15/** 16 * pre-notification 17 * @ param method18 * @ param args19 * @ param target20 * @ throws Throwable21 */22 @ Override23 public void before (Method method, object [] args, Object target) throws Throwable {24 System. out. println ("pre-notification "); 25} 26 27/** 28 * post-Notification 29 * @ param returnValue30 * @ param method31 * @ param args32 * @ param target33 * @ throws Throwable34 */35 @ Override36 public void afterReturning (Object returnValue, method method, Object [] args, Object target) throws Throwable {37 System. out. println ("post notification"); 38} 39}

Define a target object to be proxy.

1 package com. demo; 2 3 import org. springframework. stereotype. component; 4 5/** 6 * target object, the class and method to be proxy 7 * Created by Kevin on. 8 */9 @ Component ("testPoint") 10 public class TestPoint {11 12 public void test () {13 System. out. println ("method call"); 14} 15}

The purpose is to print "pre-notification" and "post-notification" before and after the test method is called ".

The notifiers defined in applicationContext. xml are as follows:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4        xmlns:context="http://www.springframework.org/schema/context" 5        xmlns:aop="http://www.springframework.org/schema/aop" 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 7  8     <context:component-scan base-package="com.demo"/> 9 10     <aop:config>11         <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>12         <aop:advisor advice-ref="advisorTest" pointcut-ref="test"/>13     </aop:config>14 15 </beans>

The final running result is as expected. The problem arises. If we only want to define this cut point <aop: pointcut id = "test" expression = "execution (* com. demo. testPoint. only pre-notifications are configured in test () "/>. What should I do at this time? The answer is, the above method is not acceptable. That is to say, if you define the Advisor method, there are some limitations. In a narrow sense, you can define only one notification and one cut-in point by defining the Advisor notification method. Of course, a notification is not accurate, because we can see that proxy can be implemented as long as different notification interfaces are implemented, but if we implement multiple notification interfaces, we cannot use only one.The notifier is a special plane.

Next, we will discuss how to use the definition plane. If you use <aop: aspect> to define a plane, the notification class does not need to implement any notification interface, which is very convenient. Similarly, to implement the functions of the above example, define a notification class, including pre-notification and post-notification.

1 package com. demo; 2 3 import org. springframework. stereotype. component; 4 5/** 6 * Created by Kevin on April /11/15. 7 */8 @ Component ("aspectTest") 9 public class AspectTest {10 11/** 12 * pre-notification 13 */14 public void doBefore () {15 System. out. println ("pre-notification"); 16} 17 18/** 19 * post-Notification 20 */21 public void doAfter () {22 System. out. println ("post notification"); 23} 24}

The target object is consistent with the preceding example, followed by the configuration of the Section in applicationContext. xml.

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4        xmlns:context="http://www.springframework.org/schema/context" 5        xmlns:aop="http://www.springframework.org/schema/aop" 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 7  8     <context:component-scan base-package="com.demo"/> 9 10     <aop:config>11         <aop:aspect ref="aspectTest">12             <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>13             <aop:before method="doBefore" pointcut-ref="test"/>14             <aop:after-returning method="doAfter" pointcut-ref="test"/>15         </aop:aspect>16     </aop:config>17 </beans>

We can see that we have defined a plane through <aop: aspect>. If you only need a pre-notification, you only need to define <aop: before>. This is the same as <aop: advisor> is very different. We can see that the notification can be flexibly defined through the <aop: aspect> definition plane method, without being as constrained as the notifier.

In fact, the notifier is a special aspect. Not mentioned in the first two blogs because the two examples use the AspectJ annotation, but the AspectJ annotation does not have the corresponding concept.

In actual use, the most common <aop: advisor> scenario is to configure transactions in Spring. It is rarely used and is not recommended. The biggest problem is that the notification interface must be implemented during notification definition, which violates the original intention of Spring non-intrusive programming.

This blog is interspersed with the source code to better clarify various concepts in Spring AOP. I have already said the reason at the beginning, and then I will officially start interpreting the source code of Spring AOP.

 

 

This is a public number that can add buff to programmers.

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.