spring learning - AOP

Keywords: Spring Junit

I. explanatory notes

  1. @Enable aspectjautoproxy: enable spring AOP proxy
  2. @Aspect: identify the current class as a tangent for the container to read
  3. @Pointcut: declare the global pointcut. The method of declaration is only the carrier of the annotation. The specific pointcut only needs to introduce the carrier.
  4. @Before: advance notice
  5. @AfterReturning: Post notification
  6. @Around: around notice, i.e. pre notice + post notice
  7. @After: final notice
  8. @AfterThrowing: exception notification

II. Realize AOP function

  • Continue with the previous test demo and create test2

  • Create interface
package com.test.spring.test3;

public interface TestAop {

    void testAop1();
}
  • Create an implementation class and register it as a Bean
package com.test.spring.test3;

import org.springframework.stereotype.Component;

@Component
public class TestAopImpl implements TestAop {

    @Override
    public void testAop1() {
        System.out.println("TestAopImpl.testAop1()");
    }
}
  • Create cut planes
package com.test.spring.test3.aop;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * The defined tangent class needs to be registered in the springIOC container, otherwise spring cannot implement aop function (because spring does not have an instance of tangent class)  
 */
@Aspect
@Component
public class FirstTangent {

    @Pointcut("execution(public * com.test.spring.test3.TestAopImpl.testAop1(..))")
    public void url(){}

    /**
     *  Before advice
     */
    @Before("url()")
    public void testBefore(){
        System.out.println("testBefore");
    }
    /**
     *  Post notification
     */
    @AfterReturning("url()")
    public void testAfterReturning(){
        System.out.println("testAfterReturning");
    }
    
 }
  • Create Config
package com.test.spring.test3;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


/**
 * @EnableAspectJAutoProxy     ---  Turn on spring AOP proxy
 */
@ComponentScan
@Configuration
@EnableAspectJAutoProxy
public class AopConfig {

}
  • Write test code
package com.test.spring.test3;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AopConfig.class)
public class Test3 {

    @Autowired
    private TestAop testAop;

    @Test
    public void TestAop(){
        testAop.testAop1();
    }
}
  • test result

III. Circular on surround

The parameter proceed ingjoinpoint is used to write a circular notice. When the pre notice is executed, the method is used to release the circular notice. When the target method is executed, the circular notice method will be returned to continue to execute the post notice.

  • Modify FirstTangent class to add surround notification method
@Aspect
@Component
public class FirstTangent {

    @Pointcut("execution(public * com.test.spring.test3.TestAopImpl.testAop1(..))")
    public void url(){}

    /**
     *  Before advice
     */
    //@Before("url()")
    public void testBefore(){
        System.out.println("testBefore");
    }

    //@AfterReturning("url()")
    public void testAfterReturning(){
        System.out.println("testAfterReturning");
    }

    //@AfterThrowing("url()")
    public void testAfterThrowing(){
        System.out.println("testAfterThrowing");
    }

    @Around("url()")
    public void testAround(ProceedingJoinPoint pjp){
        //----------Front-----------
        System.out.println("testAround1");
        
        //----------Implementation target method-----------
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        //-------------Postposition-----------------
        System.out.println("testAround2");
    }
}
  • Test surround notification

IV. use AOP to intercept comments

Just modify the global pointcut:

E.g. @ Pointcut("@annotation(com.test.spring.multi_data_source.annotation.DataSource)")

Ha ha, is it convenient to use custom annotations?

Posted by jemgames on Tue, 29 Oct 2019 13:48:47 -0700