Recently I was working on a AOP based application and was trying to put a performance measurement aspects on the service layer. With the aspect, the idea was to measure how much time it takes for the service layer to execute. The aspect looked like
public class MyAspect {
@Around("within(com.lalit.service..*)")
public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
//start the timer
Object obj = pjp.proceed();
//stop the timer and display the time
return obj;
}
}
The package structure was something like
com.lalit.service - service layer classes
com.lalit.controller - controller layer classes
com.lalit.aop - aspects
com.lalit.dao - dao layer classes.
My relevant application context was like
<!-- AspectJ Annotation Post processor -->
<aop:aspectj-autoproxy />
<!-- Register the Annotation post processor -->
<context:annotation-config/>
<!-- Package to be scanned for annotaion -->
<context:component-scan base-package="com.lalit"/>
and the relevant web application context was like
<!-- Register the Annotation post processor -->
<context:annotation-config/>
<!-- Package to be scanned for annotation -->
<context:component-scan base-package="com.lalit"/>
<!-- MVC annotation post processor -->
<mvc:annotation-driven/>
The aspect weaving did not happened, when I run it as a web application on tomcat server. However the aspect weaving was happening properly when the code is run as a part of unit test/integration test or as a stand alone application.
It seems the problem was coming because of the aspects getting processed by the web application context and not by global application context. The web application context annotation processor was processing the Aspects. It seems the aspects can only be applied to the context, which processes them. So if we need them to be applied on service layer, they should be processed by the global application context. So to fix that I changed the hierarchy as follows
com.lalit.server.service - service layer classes
com.lalit.server.aop - aspects
com.lalit.server.dao - dao layer classes.
com.lalit.web.controller - controller layer classes
All the global application context sits as part of com.lalit.server sub package and the web stuff goes in com.lalit.web.
The application context changes to
and the web application context changes to
<!-- Package to be scanned for annotation -->
<context:component-scan base-package="com.lalit.web"/>
More Articles on Spring
public class MyAspect {
@Around("within(com.lalit.service..*)")
public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {
//start the timer
Object obj = pjp.proceed();
//stop the timer and display the time
return obj;
}
}
The package structure was something like
com.lalit.service - service layer classes
com.lalit.controller - controller layer classes
com.lalit.aop - aspects
com.lalit.dao - dao layer classes.
My relevant application context was like
<!-- AspectJ Annotation Post processor -->
<aop:aspectj-autoproxy />
<!-- Register the Annotation post processor -->
<context:annotation-config/>
<!-- Package to be scanned for annotaion -->
<context:component-scan base-package="com.lalit"/>
and the relevant web application context was like
<!-- Register the Annotation post processor -->
<context:annotation-config/>
<!-- Package to be scanned for annotation -->
<context:component-scan base-package="com.lalit"/>
<!-- MVC annotation post processor -->
<mvc:annotation-driven/>
The aspect weaving did not happened, when I run it as a web application on tomcat server. However the aspect weaving was happening properly when the code is run as a part of unit test/integration test or as a stand alone application.
It seems the problem was coming because of the aspects getting processed by the web application context and not by global application context. The web application context annotation processor was processing the Aspects. It seems the aspects can only be applied to the context, which processes them. So if we need them to be applied on service layer, they should be processed by the global application context. So to fix that I changed the hierarchy as follows
com.lalit.server.service - service layer classes
com.lalit.server.aop - aspects
com.lalit.server.dao - dao layer classes.
com.lalit.web.controller - controller layer classes
All the global application context sits as part of com.lalit.server sub package and the web stuff goes in com.lalit.web.
The application context changes to
<!-- Package to be scanned for annotation -->
<context:component-scan base-package="com.lalit.server"/>
and the web application context changes to
<!-- Package to be scanned for annotation -->
<context:component-scan base-package="com.lalit.web"/>
More Articles on Spring
No comments:
Post a Comment