Java – does AspectJ not capture all events in the spring framework?
My project is based on spring framework 2.5 4. I try to add aspects to some controllers (I use AspectJ 1.5.3)
I'm at application servlet Automatic proxy is enabled in XML, just paste these lines to the end of the XML file:
<aop:aspectj-autoproxy /> <bean id="auditLogProcessor" class="com.example.bg.web.utils.AuditLogProcessor" />
Create aspect:
package com.example.bg.web.utils; import org.apache.log4j.Logger; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.pointcut; @Aspect public class AuditLogProcessor { private final static Logger log = Logger.getLogger(AuditLogProcessor.class); @After("execution(* com.example.bg.web.controllers.assets.AssetThumbnailRebuildController.rebuildThumbnail(..))") public void afterHandleRequest() { log.info("test111"); } @After("execution(* com.example.bg.web.controllers.assets.AssetThumbnailRebuildController.rebuildThumbnail(..))") public void afterRebuildThumbnail() { log.info("test222"); } }
My controller:
class AssetAddController implements Controller class AssetThumbnailRebuildController extends MultiActionController
When I set the brake point in the aspect advisor and call controller, I only capture afterhandlerequest() instead of afterrebildthumbnail(). What did I do wrong?
be careful
I ask this question on behalf of my friend. His friend can't access so beta, and I don't know what it is
edit
There are some spelling mistakes, thank you cheekysoft But the problem remains
Solution
Your breakpoint was not hit because you are using spring's AOP proxies See understanding AOP proxies for a description of how AOP proxies are special
Basically, the MVC framework will call the handlerequest method on the controller agent (for example, the multi actioncontroller you use as the base class implementation), and then this method will make an "internal" call to its rebuildthumbnail method, but this will not pass through the agent, so it will not receive any aspects (this has nothing to do with the final method.)
To achieve the effect you want, investigate the use of "real" AOP by loading time weaving (spring support is very good)