我有这个注释和一个方面类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AroundHere {
}
@Aspect
@Component
public class SomeAspect {
@Around("@annotation(com.annotation.AroundHere)")
public Object redirectIfNotEditingStatus(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("hi");
return pjp.proceed();
}
}
我想在某些带有 @AroundHere
注释的方法上打印“hi”。它在服务层上工作正常,但在 Controller 上不起作用。我怀疑 Spring Security,因为它会扫描所有 Controller 组件。
如何让它们在 Controller 上工作?
最佳答案
很可能它对您不起作用,因为 aop:aspectj-autoproxy
是在一个应用程序上下文中定义的,而您的 Controller 在不同的应用程序上下文中。通常,BeanFactoryProcessors
(当 aop:aspectj-autoproxy
在上下文中时,它们正在执行实际工作)仅对定义它们的容器中的 bean 起作用。因此,例如,如果您在根应用程序上下文中定义了 aspectj-autoproxy
,它将不会作用于 *-servlet.xml
上下文中定义的 bean。
您可以找到与此主题相关的相关文档 here :
BeanPostProcessors are scoped per-container. This is only relevant if you are using container hierarchies. If you define a BeanPostProcessor in one container, it will only post-process the beans in that container. In other words, beans that are defined in one container are not post-processed by a BeanPostProcessor defined in another container, even if both containers are part of the same hierarchy.
关于java - 带有 Spring Security 的 AspectJ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259732/