我(当然)试图使用许多我不太了解的结构来维护项目.在试图弄清楚Spring中的AOP使用的过程中,我遇到了带有以下注释的方法:
@Around(value ="@ annotation(annotation)")
所以@Around意味着我们在AOP中执行方法切入点的'around'版本,我明白了.我不知道其他部分是什么意思.Spring文档提供以下内容:
@annotation - 限制连接点的匹配,其中连接点的主题(在Spring AOP中执行的方法)具有给定的注释
我不知道这意味着什么 - "在Spring AOP中执行的方法"听起来像建议的方法,但我不知道我(或Spring)如何找出建议的方法.听起来它是具有"给定注释"的方法,但如果是这样,那么给出了什么注释?
这个注释建议了哪些方法?还有什么意思呢?
人们经常会问AspectJ这样的问题,所以我想在以后可以轻松链接的地方回答.
我有这个标记注释:
package de.scrum_master.app;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Marker {}
Run Code Online (Sandbox Code Playgroud)
现在我注释一个接口和/或方法,如下所示:
package de.scrum_master.app;
@Marker
public interface MyInterface {
void one();
@Marker void two();
}
Run Code Online (Sandbox Code Playgroud)
这是一个小驱动程序应用程序,它也实现了接口:
package de.scrum_master.app;
public class Application implements MyInterface {
@Override
public void one() {}
@Override
public void two() {}
public static void main(String[] args) {
Application application = new Application();
application.one();
application.two();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我定义这个方面时,我希望它被触发
package de.scrum_master.aspect;
import de.scrum_master.app.Marker;
public aspect MarkerAnnotationInterceptor {
after() : execution((@Marker *).new(..)) && …Run Code Online (Sandbox Code Playgroud) 在春季介绍之前@GetMapping,我们只关心一个注释@RequestMapping,因此,此方面有效
@Before("within(aa.bb.*.rest..*) && execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
Run Code Online (Sandbox Code Playgroud)
但是,在我们可以使用@GetMapping之后@PostMapping,这一点不起作用,但是这些注释具有meta注释@RequestMapping。
有什么办法可以轻松地拦截所有@RequestMapping/ @{Get,Post,Put,Patch,..}Mapping?