方法级别的自定义注释在 spring boot 中不起作用

时间:2021-05-20 11:30:05

标签: spring-boot spring-aop

我正在尝试定义自定义注释(LogMe),它应该在用注释修饰的方法之前和之后运行。 注释适用于 spring 识别的方法 - 使用 @GetMapping 等定义的方法。但是,我自定义编写的方法上的注释不会调用 AOP。

我定义的注解如下:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogMe {

我的方面是这样的:

@Configuration
@EnableAspectJAutoProxy
@Aspect
public class LogMeAspect {


    @Pointcut("@annotation(com.api.logging.aspect.LogMe)")
    public void loggableMethods() {}


    @Around("loggableMethods()")
    public Object serviceResponseTimeAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println(">>>>>>>>>>>REACHING TO PJP:"+joinPoint.getSignature().getName());
        Object obj = joinPoint.proceed();
        return obj;
    }

}

spring.factories 定义如下(我试图从依赖访问这个方面)。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.api.logging.aspect.LoggingAspect,\
com.api.logging.aspect.LogMeAspect

注解的使用:

public class Utils {
    @LogMe
    public String testing(int i, int j, String str) {
        System.out.println("in testing");
        testing2();
        return i+j+str;
    }

    @LogMe
    public void testing2() {
        System.out.println("in testing 2");
    }

我的方面模块有以下依赖项。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback.contrib</groupId>
            <artifactId>logback-json-classic</artifactId>
            <version>${logback.contrib.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback.contrib</groupId>
            <artifactId>logback-jackson</artifactId>
            <version>${logback.contrib.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>6.4</version>
        </dependency>

我在调用方面时对模块有以下依赖项。当然,其中之一就是aspect依赖——spring-boot-api-logging

        <!-- core -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- matrix -->

        <!-- kafka -->

        <!-- tests -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- database -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- logging-->
        <dependency>
            <groupId>org.api.commons.logging</groupId>
            <artifactId>spring-boot-api-logging</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

0 个答案:

没有答案