Take You Fast to Advance with Spring AOP

Spring AOP-Advanced

Direct application instructions: Schedule!!!

Advanced Case 1: Surround Notification

@Around: surround notifications.Execute code before the connection point executes

After the connection point executes, execute the code

Controls whether the connection point is executed.(default connection point is not executed in surround notification)

 

code implementation

Configuration class:

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

 

Face class:

@Component
@Aspect
public class MyAspect {

    @Around("execution(* com.czxy.demo3.service..*.*(..))")
    public void a1(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Around Advice--1");
        //In wrapping notifications, if a connection point is to be executed, the proceed method of the ProceedingJoinPoint object needs to be executed
        //Return value of proceed method = Return value of connection point.Connection point return value type is void, where return value is null
        Object proceed = pjp.proceed();
        System.out.println("Return value of connection point:"+proceed);
        System.out.println("Around Advice--2");
    }
}

 

Service class:

@Service
public class Demo3ServiceImpl implements Demo3Service {
    @Override
    public void run1() {
        System.out.println("run1 Executed");
    }
    @Override
    public String run2() {
        System.out.println("run2 Executed");
        return "Ha ha ha";
    }
}

 

 

 

Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class Demo3Test {
    @Resource
    private Demo3Service demo3Service;
    @Test
    public void run1(){
        demo3Service.run1();
        System.out.println("++++");
        demo3Service.run2();
    }
}

 

summary

@Around: surround notifications.

Before the connection point executes, execute the code

After the connection point executes, execute the code

Controlling whether connection points are executed

 

If an impl class has no interface inheritance

 

Writable directly in test without error

Otherwise, if you write the error directly

 

 

 

 

2. Advanced Case 2: Exception Notification

Sketch

@AfterThrowing: Exception notification.Executes only when an exception occurs at the connection point.

 

code implementation

Configuration class:

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

Face class:

@Component
@Aspect
public class MyAspect {

    @AfterThrowing(value="execution(* com.czxy.demo1.service..*.*(..))",throwing = "ex")
    public void a1(JoinPoint joinPoint,Throwable ex){
        System.out.println("Exception notification:"+joinPoint.getSignature().getName()+"Exception in method"+ex.getMessage());
    }
}

 

Service:

 

@Service
public class Demo1ServiceImpl implements Demo1Service {
    @Override
    public void test01() {
        System.out.println("---test01---");
    }
    @Override
    public void test02() {
        System.out.println("---test02---");
        int a = 1/0;
    }
}

 

Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class Demo1Test {
    @Resource
    private Demo1Service demo1Service;
    @Test
    public void run1(){
        demo1Service.test01();
        System.out.println("++++++");
        demo1Service.test02();
    }
}

 

 

Advanced Case 3: Post Notification

Sketch

@AfterReturning: Post Notification.Executed after the connection point execution.

Connection point return values are available.

Return null if the connection point does not return a value

 

 

code implementation

Face class:

@Component
@Aspect
public class MyAspect {

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

 

 

Service:

@Service
public class Demo1ServiceImpl implements Demo1Service {
    @Override
    public void test01() {
        System.out.println("---test01---");
    }
    @Override
    public void test02() {
        System.out.println("---test02---");
        int a = 1/0;
    }
    @Override
    public String test03() {
        System.out.println("---test03---");
        return "QAQ";
    }
}

 

Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class Demo1Test {
    @Resource
    private Demo1Service demo1Service;
    @Test
    public void run2(){
        demo1Service.test01();
        System.out.println("++++++");
        demo1Service.test03();
    }
    @Test
    public void run1(){
        demo1Service.test01();
        System.out.println("++++++");
        demo1Service.test02();
    }
}

 

 

Advanced Summary

@Before: Pre-notification.Execute before the connection point executes.

@Around: surround notifications.Controls whether the connection point is executed.

@After: Final Notification.Executed after the connection point is executed, in any case.Equivalent to finally

@AfterThrowing: Exception notification.Execution occurs only after the connection point has an exception.

Throwable

@AfterReturning: Post Notification.Executes after the connection point executes.Get the return value of the connection point

 

JoinPoint: Connection point encapsulation object

 

 

 

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!!

​​​​​​​

Tags: Programming Spring

Posted on Tue, 05 May 2020 07:19:44 -0700 by misc00500