java - 带有 Spring Security 的 AspectJ

标签 java spring spring-mvc spring-security aspectj

我有这个注释和一个方面类

@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/

相关文章:

java - 错误[org.jboss.msc.service.fail](MSC服务线程1-3)MSC000001 : Failed to start service jboss.部署.单元。 .POST_MODULE

java - 使用 Spring MVC 为 @RequestBody 使用泛型类型时出现 "argument type mismatch"错误

spring - Spring 安全性中的多个 antMatchers

spring-mvc - 无状态的 Spring Security oauth2 提供者

java - Java中按大写字母分割

java - 使用 Ehcache 进行网页缓存

java - 使用 Hibernate for ORM 的抽象/具体类的 MySQL 表

java - 一旦 jLine 将一行写入 shell,我们就无法返回到原始行

java - Spring Integration JPA 入站 channel 适配器

java - Spring RMI刷新StubOnConnectFailure不工作