The article directories

  • Some terms for AOP
  • AOP concrete implementation

Some terms for AOP

Join points: method pointcuts that can be enhanced in a class: Methods that are actually enhanced in a class Advice: Part of the process of applying advice to a pointcut. Advice commonly used are pre-advice, post-advice, and wrap advice

AOP concrete implementation

Spring AOP is implemented based on AspectJ, so dependencies need to be introduced first

< the dependency > < groupId > org. Aspectj < / groupId > < artifactId > aspectjweaver < / artifactId > < version > 1.9.4 < / version > </dependency>Copy the code

We do this with annotations, so turn on annotations, then turn on the annotations scan package, and also turn on Aseptic generate proxy objects

<! <context:annotation-config/> <! <context:component-scan base-package="com.hzy.aopanno"/> <! < AOP :aspectj-autoproxy/>Copy the code

Now that we’re ready to apply, if we want to pre-notify the Add method under the User class, that is, to do something before executing the method, first create a User class and inject it into Spring

package com.hzy.aopanno;

import org.springframework.stereotype.Component;

@Component
public class User {
    public void add() {
        System.out.println("add ...");
    }
}
Copy the code

Then there is a proxy class for User, which is responsible for notifications, where @Aspect is the Aspect, and applies that notification to the pointcut, where it is executed (* com.hzy.aopanno.user.add (..)). ) represents the execution of the add method if all methods are executed as execution(* com.hzy.aopanno.User.*(..)). )

package com.hzy.aopanno; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect public class UserProxy { @Before(value = "execution(* com.hzy.aopanno.User.add(..) )") public void before() { System.out.println("before ..." ); }}Copy the code

So let’s test that out

import com.hzy.aopanno.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = context.getBean("user", User.class); user.add(); }}Copy the code



And then we’ll demonstrate after and around

Modifying the proxy class

package com.hzy.aopanno; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect public class UserProxy { @Before(value = "execution(* com.hzy.aopanno.User.add(..) )") public void before() { System.out.println("before ..." ); } @After(value = "execution(* com.hzy.aopanno.User.add(..) )") public void after() { System.out.println("after ..." ); } @Around(value = "execution(* com.hzy.aopanno.User.add(..) )") public void around(ProceedingJoinPoint ProceedingJoinPoint) throws Throwable {system.out.println ("...") ); proceedingJoinPoint.proceed(); System.out.println(" after wrapping...") ); }}Copy the code

If you look at the test, it goes around before, it goes around after