@AspectJ pointcut for execute methods of a package

2019-07-15 10:43发布

问题:

I want to execute a execute method in a specific package.

What could be a possible pointcut for this?

Note: I am using @AspectJ style Spring AOP.

回答1:

Have a look here http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations-pointcuts-and-advice.html

@(org.xyz..*) Matches any annotated element which has either an annotation of a type matching the type pattern (org.xyz..*). In other words, an annotated element with an annotation that is declared in the org.xyz package or a sub-package. (The parenthesis are required in this example).

So you should have the following aop config:

<aop:config>
 <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.mycompany..*(..))" order="1"/> 
</aop:config>

and matching bean for this advice

<bean id="myadvice" class="com.mycompany.MyIntercetpor"/>

Interceptor should implement org.aopalliance.intercept.MethodInterceptor