Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP: passing target to Aspect

I want to catch exceptions thrown by classes implementing Processor interface. In aspect I need an access to processor, which throws the exception. I define following pointcut:

@Pointcut("target(some.package.Processor) && args(message)")
  public void processor(Message message) {
}

And aspect:

@AfterThrowing(pointcut="processor(message)", throwing="ex")
public void onExceptionInProcessor(Processor target, Exception ex, Message message) {
  // code skipped
}

However, I get following exception:

Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301) ~[aspectjweaver-1.6.12.jar:1.6.12]
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:193) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:182) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:163) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:209) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:263) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:295) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:117) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:359) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322) ~[spring-aop-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1461) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    ... 58 common frames omitted

What is a correct way of binding target object to aspect? The only way I've found is binding JoinPoint, but it suggests following:

Unless you specifically need this reflective access, you should use the target pointcut designator to get at this object for better static typing and performance

Does anyone one how to do this?

like image 812
kazaand Avatar asked Mar 28 '12 09:03

kazaand


People also ask

What is the difference between JoinPoint and PointCut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

What is aspect in AOP Spring?

Aspect: An aspect is a class that implements enterprise application concerns that cut across multiple classes, such as transaction management. Aspects can be a normal class configured through Spring XML configuration or we can use Spring AspectJ integration to define a class as Aspect using @Aspect annotation.


1 Answers

Your target should refer to one of the parameter names in the method. For example saying

@Pointcut("target(myProc) && args(message)")
public void processor(Processor myProc, Message message) {
}

will give you the Processor in type safe way in myProc variable.

like image 73
vertti Avatar answered Sep 18 '22 18:09

vertti