Java RestController父类的方面方法不会触发通知逻辑
我有一些构造,我真的很想在上面使用方面。我想让我的RestController从一个类继承,该类生成特殊的日志记录方法Java RestController父类的方面方法不会触发通知逻辑,java,spring-mvc,logging,spring-aop,spring-restcontroller,Java,Spring Mvc,Logging,Spring Aop,Spring Restcontroller,我有一些构造,我真的很想在上面使用方面。我想让我的RestController从一个类继承,该类生成特殊的日志记录方法 记录到标准的logback输出 向处理日志的服务激发http请求 消息也(由方面完成) 我创建了一个注释,我用它标记了我想显示的方法,这样切入点就不能过滤它。特殊情况是,此方法在RestController的父类中声明 即使IntelliJ将该方法标记为该方面正在使用,该方面也没有运行,这告诉我切入点必须工作 请查看我的代码,并检查我可能遗漏了什么以使其正常工作 应用程序类
@SpringBootApplication
@ComponentScan("com.xetra.experimental")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopTryoutApplication {
public static void main(String[] args) {
SpringApplication.run(AopTryoutApplication.class, args);
}
}
public interface EndpointLogger {
void log(String msg);
}
RestController
@RestController
public class Endpoint extends SimpleLogger {
@GetMapping("/endpoint")
public void doStuff(){
log("foo");
}
}
public class SimpleLogger implements EndpointLogger{
@AspectAnnotation
public void log(String msg) {
System.out.println(msg);
}
}
RestController的父类
@RestController
public class Endpoint extends SimpleLogger {
@GetMapping("/endpoint")
public void doStuff(){
log("foo");
}
}
public class SimpleLogger implements EndpointLogger{
@AspectAnnotation
public void log(String msg) {
System.out.println(msg);
}
}
父类的接口(听说aspected方法需要接口)
注释我的方面应该切入点
@Inherited
public @interface AspectAnnotation {
}
春季AOP方面
@Component
@Aspect
public class TestAspect {
@Pointcut("@annotation(com.xetra.experimental.aoptryout.AspectAnnotation)")
public void methods() {
}
@Before("methods()")
public void beforeMethodExecution(JoinPoint jp) {
System.out.println("Aspect ran!!!!");
}
}
由于Spring的AOP框架基于代理的特性,根据定义,目标对象中的调用不会被拦截
你可以找到更多
对log
方法的调用不会被拦截,因为它是从属于同一目标对象的doStuff
方法发出的
现在,只要对log
方法的任何调用是从另一个对象(不是同一个目标对象)外部发出的,就会被拦截
问题
因此,如果我将SimpleLogger用作一个组件而不是端点内的父类,它将正常工作?
是的,你说得对
有什么办法可以让它发挥作用吗?喜欢使用AspectJ而不是Spring AOP吗?
您可以使用AspectJ的源代码编织使其正常工作,是一个工作示例。因此,如果我将
SimpleLogger
用作一个组件,而不是端点内的父类,它将工作一天?是否有任何方法使其工作?像使用AspectJ而不是Spring AOP?更新了我的帖子,回答了您的问题。