![](/img/trans.png)
[英]Spring Boot Separate modules for api, interface and implementation
[英]Spring boot interceptor for implementation of interface
您好,在 spring 中有没有办法为每个实现特定接口的 class 创建拦截器?
每次调用某个接口的实现的类的任何方法(甚至不是来自接口的方法)时,我都需要执行一些代码
可以说我有
public interface Animal {
public void makeSound();
}
public class Dog implements Animal {
public void makeSound(){
.....
}
public void bite(Human human) {
...
}
}
是否有任何方法可以在执行 makeSound、bite 或任何其他方法时执行某些代码,或者在拦截器本身中获取这些方法的参数?
谢谢
你可以使用一些aop编程。
你必须像这样定义一个切入点:
@Aspect
public class AnimalAspect {
@Pointcut("execution(* com.example.animals.Animal.*(..))")
public void onAnimalMethodInvoked() {}
@After("onAnimalMethodInvoked()")
public void doAccessCheck() {
// ... put your code here
}
}
您可以在https://docs.spring.io/spring-framework/docs/2.0.x/reference/aop.html阅读更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.