Sanghyun Lee
Sanghyun Lee

Reputation: 23002

AspectJ with Spring Security

I've got this annotation and an aspect class

@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();
    }
}

I want to print "hi" on some methods which has the @AroundHere annotation. It works fine on the service layer, but not working on the contollers. I suspect Spring Security because it scans all the controller components.

How can I make them work on the controllers?

Upvotes: 1

Views: 912

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Most probably it's not working for you because aop:aspectj-autoproxy is defined in one app context and you have your controllers in a different app context. As a rule, BeanFactoryProcessors (which are doing the actual work when aop:aspectj-autoproxy is in the context) act only on the beans in the container where they are defined. So, for example, if you have aspectj-autoproxy defined in the root app context, it will not act on the beans defined in *-servlet.xml context.

You can find the relevant piece of documentation related to this subject 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.

Upvotes: 3

Related Questions