Dave Taubler
Dave Taubler

Reputation: 1091

Spring Aspects not executing

I've been working on adding aspects to a Spring MVC webapp, and the aspects aren't executing. I've tried to boil it down to something dirt simple that clearly should work, but still no go. Here's where I'm at now:

// imports...
@Aspect
public class AuthCheckerAspect {
    {
        System.out.println("initting");
    }

    @Pointcut("execution(* * *(..))")
    public void c() {}

    @Before("c")
    public void cc(JoinPoint pjp) throws Throwable {
        System.out.println("test...");
    }
 }

As far as I can tell, the pointcut should apply to any method in any Spring-managed class (of which there are a lot in my application). I also added the following to my Spring config:

<aop:aspectj-autoproxy/>

I set a breakpoint at the System.out.println() statement in the cc() method, but it never caught (and yes, I'm positive that the debugger is attached; other breakpoints catch properly). I'm suspecting that the AuthCheckerAspect class is never getting loaded into the Spring context, because I also set a breakpoint within the initializer clause, and that never catches either; when I do the same with other Spring-managed classes, their breakpoints always catch during app startup.

Is there something else I need to be doing?

Thanks in advance.

Upvotes: 4

Views: 6116

Answers (2)

madhead
madhead

Reputation: 33412

Spring does not automatically mange @Aspects. Add <bean class="AuthCheckerAspect" /> to your context or annotate it with @Component and include in component scan path.

Upvotes: 9

ruhsuzbaykus
ruhsuzbaykus

Reputation: 13380

add this to your configuration file:

<context:annotation-config />
<context:component-scan base-package="root.package.to.be.scanned" />
<aop:aspectj-autoproxy>
    <aop:include name="nameOfAspectBean" />
</aop:aspectj-autoproxy>

Upvotes: 3

Related Questions