SpringBoot-方面无法在RestController中识别常规方法

时间:2019-12-26 09:03:06

标签: java spring spring-restcontroller aspect

我有一个带有RestController和Aspect的简单SpringBoot应用程序

配置:

package test.SpringTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class SpringTestConfig {
}

注释:

package test.SpringTest;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target(METHOD)
@Retention(RUNTIME)
public @interface TestAnnotation {
}

方面:

package test.SpringTest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;

@Aspect
@Component
@RequiredArgsConstructor
public class TestAspect {
    @Before("@annotation(test.SpringTest.TestAnnotation)")
    void testAspect(JoinPoint joinPoint) {
        System.out.println("Before aspect");
        System.out.println(joinPoint.getSignature());
        System.out.println("End aspect");
    }
}

RestController:

package test.SpringTest;
import static org.springframework.http.ResponseEntity.ok;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class TestController {
    @PostMapping(value = "/test/testPost")
    public ResponseEntity<String> testPost(){
        return ok(innerMethod());
    }

    @TestAnnotation
    public String innerMethod() {
        return "bla bla bla";
    }
}

当注释位于request方法-testPost上方时,我看到了方面的输出。当注释位于inner方法之上时,该方面的输出不会被打印。

为什么在内部方法上标注时未触发方面?

0 个答案:

没有答案