spring learning (three)-AOP

1. Annotation

  1. @EnableAspectJAutoProxy : Enable springAOP proxy function
  2. @Aspect : Identifies the current class as an aspect for the container to read
  3. @Pointcut : Declare the global entry point, the declared method is only the carrier of the annotation, the specific entry point only needs to be introduced into the carrier
  4. @Before : pre-notification
  5. @AfterReturning: Post notification
  6. @Around: Surround notification means: front notification + rear notification
  7. @After: Final notice
  8. @AfterThrowing: exception notification

Second, realize AOP function

  • Import the jar package
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.2</version>
</dependency>         
  • Continue to 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 aspect
package com.test.spring.test3.aop;

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

/**
 * 需要将定义的切面类注册到springIOC容器中,否则spring无法实现aop功能(因为spring没有切面类的           实例)  
 */
@Aspect
@Component
public class FirstTangent {

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

    /**
     *  前置通知
     */
    @Before("url()")
    public void testBefore(){
        System.out.println("testBefore");
    }
    /**
     *  后置通知
     */
    @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     ---  开启springAOP代理功能
 */
@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 Results

3. About the surround notification

The parameter ProceedingJoinPoint is needed to write the surround notification. After the current notification is executed, use the proceed method to release. When the target method is executed, it will return to this notification method and continue to execute the post notification

  • 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("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){
        //----------前置-----------
        System.out.println("testAround1");
        
        //----------执行目标方法-----------
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        //-------------后置-----------------
        System.out.println("testAround2");
    }
}
  • Test surround notification

Fourth, use AOP to intercept annotations

Just modify the global entry point:

      如:@Pointcut("@annotation(com.test.spring.multi_data_source.annotation.DataSource)")

      Haha, is it much more convenient when using custom annotations

Guess you like

Origin blog.csdn.net/qq_42407917/article/details/95619407