Getting Started with Spring Framework 4: Using AOP Based on Annotation

1. Brief introduction

The implementation of AOP based on XML configuration has been discussed earlier. This article briefly talks about the implementation based on annotations.

Before the annotation-based method is implemented, the annotation method injection must be enabled by configuring aop:aspectj-autoproxy in the xml configuration.

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
       http://www.springframework.org/schema/aop   
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
          
        <aop:aspectj-autoproxy/>  
/>  
     

Of course, this step can also be achieved through annotations, let's look at the code.

2. Steps

  1. Introduce dependencies

    As mentioned above, first introduce the dependencies of Spring-Aop and AspectJ

  

        <!--测试1使用-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--测试2、3、4、5、6使用-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--测试Aop使用-->
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>

  2. Create the following classes under /src/test/java/aoptest2:

package aoptest2;

import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Component
@ComponentScan()
@EnableAspectJAutoProxy(exposeProxy = true ) // AOP that needs to enable annotation 
public  class MyTeacher {
    @Pointcut("execution(* aoptest2.MyTeacher.aopPointMethod1(..))")
    public void aopPointMethod1() {
        System.out.println("this is aopPointMethod1 executed.");
    }
}
package aoptest2;

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

@Aspect
@Component
public class MyTeacherExtension {
    @Before("aoptest2.MyTeacher.aopPointMethod1()")
    public void aopInspectAtBefore() {
        System.out.println("this is aopInspectAtBefore method execute.");
    }

    @AfterReturning("aoptest2.MyTeacher.aopPointMethod1()")
    public void aopInspectAtAfterReturing() {
        System.out.println("this is aopInspectAtAfterReturing method execute.");
    }
    @After("aoptest2.MyTeacher.aopPointMethod1()")
    public void aopInspectAtAfter() {
        System.out.println("this is aopInspectAtAfter method execute.");
    }
    @Around("aoptest2.MyTeacher.aopPointMethod1()")
    public void aopAround(ProceedingJoinPoint proceedingJoinPoint) {
        try {
            System.out.println("aopAround1");
            Object obj = proceedingJoinPoint.proceed();
            System.out.println("aopAround2");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


}

 

  3. Add a test class:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public  class AopTest {
      @Test
    public  void aopTest2 () {
        ApplicationContext context = new AnnotationConfigApplicationContext("aoptest2");
        aoptest2.MyTeacher mywoker = context.getBean(aoptest2.MyTeacher.class);
        mywoker.aopPointMethod1 ();
    }
}

  4. Run the test

aopAround1
this is aopInspectAtBefore method execute.
this is aopPointMethod1 executed.
aopAround2
this is aopInspectAtAfter method execute.
this is aopInspectAtAfterReturing method execute.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325657378&siteId=291194637