Spring AOP - Advanced Details

Keywords: Programming Spring

;

Spring AOP-Advanced

Come on, go straight to the case description: Whoop.....

1. Advanced Case 1: Defining a public entry point

Sketch

There are many notifications in the facet class, most of which have the same entry point.

For ease of entry point management and maintenance, use a common entry point.

@Pointcut

Method modifier:private

Return value of method: void

Method has no parameters

Method has no method body

 

code implementation

Face class: (Note: Marking yellow is based on the code added last time)

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(* com.czxy.demo1.service..*.*(..))")
    private void myCut1(){}

    @AfterThrowing(value="myCut1()",throwing = "ex")
    public void a1(JoinPoint joinPoint,Throwable ex){
        System.out.println("Exception notification:"+joinPoint.getSignature().getName()+"Exception in method"+ex.getMessage());
    }
    @AfterReturning(value="myCut1()",returning = "obj")
    public void a2(JoinPoint joinPoint,Object obj){
        System.out.println("Post Notification:"+joinPoint.getSignature().getName()+"The method return value is:"+obj);
    }
}

 

 

2. Advanced Case 2: Define a complete tangent class

code implementation

Configuration class:

@Configuration
@ComponentScan(basePackages = {"com.czxy.demo2"})
@EnableAspectJAutoProxy
public class SpringConfiguration {
}

 

Face class:

@Component
@Aspect
public class MyAspect {
    /*
    * Define a common entry point
    * */
    @Pointcut("execution(* com.czxy.demo2.service..*.*(..))")
    private void myCut1(){}
    /*
    * Around Advice
    * */
    @Around("myCut1()")
    public void a1(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Around Advice---Before Connection Point Execution");
        Object proceed = pjp.proceed();
        System.out.println("In surround notification-Connection point return value:"+proceed);
        System.out.println("Around Advice---After connection point execution");
    }
    /*
    * Before advice
    * */
    @Before("myCut1()")
    public void b1(JoinPoint joinPoint){
        System.out.println("Before advice---"+joinPoint.getSignature().getName());
    }

    /*
    * Final Notification
    * */
    @After("myCut1()")
    public void a2(JoinPoint joinPoint){
        System.out.println("Final Notification---"+joinPoint.getSignature().getName());
    }

    /*
    * after returning advise
    * */
    @AfterReturning(value="myCut1()",returning = "obj")
    public void a3(JoinPoint joinPoint,Object obj){
        System.out.println("after returning advise---"+joinPoint.getSignature().getName());
    }

    /*
    * Exception Notification
    * */
    @AfterThrowing(value="myCut1()",throwing = "ex")
    public void a4(JoinPoint joinPoint,Throwable ex){
        System.out.println("Exception Notification---"+joinPoint.getSignature().getName()+"The exception is:"+ex.getMessage());
    }
}

 

Service:

@Service
public class Demo2ServiceImpl implements Demo2Service {
    @Override
    public void test01() {
        System.out.println("---test01---");
    }
    @Override
    public String test02() {
        System.out.println("---test02---");
        return "Ouch";
    }
    @Override
    public void test03(String a, String b) {
        System.out.println("---test03---");
        System.out.println("parameter a:"+a+"\t parameter b:"+b);
    }
    @Override
    public void test04() {
        System.out.println("---test04---");
        int a=1/0;
    }
}

 

Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class Demo2Test {
    @Resource
    private Demo2Service demo2Service;

    @Test
    public void run1(){
        demo2Service.test01();
        System.out.println("++++++++");
        demo2Service.test02();
        System.out.println("++++++++");
        demo2Service.test03("aa","bb");
        System.out.println("++++++++");
        demo2Service.test04();
        System.out.println("++++++++");
    }
}

 

Order of Notification Execution [Key]

 

 

 

3. Advanced Case 3: Combination of Post Notification and Surround Notification (Return Value Transfer)

code implementation

Face class

@Component

@Aspect
public class MyAspect {
    /*
    * Define a common entry point
    * */
    @Pointcut("execution(* com.czxy.demo2.service..*.*(..))")
    private void myCut1(){}
    /*
    * Around Advice
    * */
    @Around("myCut1()")
    public Object a1(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Around Advice---Before Connection Point Execution");
        Object proceed = pjp.proceed();
        System.out.println("In surround notification-Connection point return value:"+proceed);
        System.out.println("Around Advice---After connection point execution");
        return proceed;
    }
    /*
    * Before advice
    * */
    @Before("myCut1()")
    public void b1(JoinPoint joinPoint){
        System.out.println("Before advice---"+joinPoint.getSignature().getName());
    }

    /*
    * Final Notification
    * */
    @After("myCut1()")
    public void a2(JoinPoint joinPoint){
        System.out.println("Final Notification---"+joinPoint.getSignature().getName());
    }

    /*
    * after returning advise
    * */
    @AfterReturning(value="myCut1()",returning = "obj")
    public void a3(JoinPoint joinPoint,Object obj){
        System.out.println("after returning advise---"+joinPoint.getSignature().getName()+"The return value is:"+obj);
    }

    /*
    * Exception Notification
    * */
    @AfterThrowing(value="myCut1()",throwing = "ex")
    public void a4(JoinPoint joinPoint,Throwable ex){
        System.out.println("Exception Notification---"+joinPoint.getSignature().getName()+"The exception is:"+ex.getMessage());
    }
}

 

 

Summary;

SpringAOP: Face-oriented programming.Insert the new code into the execution of the original code without modifying the original code.

Connection Points: Method Intercepted by spring

Start point: Usually an expression.Indicates which connection points spring needs to intercept

Notification: What to do after intercepting a connection point

Aspects: Contains entry points and notifications.@Aspect

 

@Around: surround notifications.Controls whether connection points are executed

@Before: Pre-notification.

@After: Final Notification.Notification that will be executed anyway

@AfterReturning: Post Notification.Gets the return value of the connection point.(If a surround notification is set, the surround notification must pass the connection point return value as a return value)

@AfterThrowring: Exception notification.Only when the connection point has an exception.

 

 

 

 

 

Congratulations on your progress!!!

The more you know, the more you don't know!

~Thank you for reading like-minded, your support is my greatest motivation to learn! Go on, strangers work together, reluctantly!!

Posted by furiousweebee on Mon, 04 May 2020 20:09:46 -0700