lalit
lalit

Reputation: 1475

Spring AOP does not works in Tomcat and tcserver

I have an aspect which works fine when I run it from a unit test or through a stand alone application. However when I run it as a part of web application and host it on Tomcat the Aspects are not applied.

My aspect looks like

public class MyAspect {

    @Around("within(com.service..*)")
    public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
        //do something
        Object obj = pjp.proceed();
        //do something else
        return obj;
    }

}

Upvotes: 3

Views: 2555

Answers (3)

Matt.Lee
Matt.Lee

Reputation: 1

How about move

<context:component-scan base-package="com.*" />
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />   

to servlet-mvc.xml?

Upvotes: 0

lalit
lalit

Reputation: 1475

I am able to solve this. The reason was that the aspect were getting processed by web application context and not by global application context so I have to restructure couple of things. I have detailed the steps here

@seanizer Spring does support within. It's true that it is only applied to methods and in within it will apply to methods of all the package and sub package of com.service. For details check the reference documentation here

Upvotes: 3

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298908

Update: I'll leave this in, because it's still partially valid, even if it didn't help in your case. I'll edit a few places though, edits are marked like this or this.

If you're using Spring AOP, it can't work. Spring AOP only fully supports the execution pointcut. The within pointcut only works when it applies to method executions, for the full functionality of within you will need AspectJ (Spring AOP only uses some AspectJ pointcuts, but not the AspectJ weaver). Either through static compilation (usually through Maven or Ant) or through Load-Time-Weaving.

Also, your class is missing an @Aspect annotation.

Upvotes: 1

Related Questions