在使用AOP时,指定一个pointcut的同时会定义一个expression,来表示对什么方法使用AOP。
类似expression="execution(public * com.baidu.map.*Ctr.*Handle(..))"等
语法为:execution(修饰符 返回值 包名.类名/接口名.方法名(参数列表))
其中:
1、返回类型、方法名、参数是必须有的.
2、*表示任意值. 比如返回类型,方法名等.
3、(..)可以代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个参数为String类型.
举例说明:
1、任意公共方法的执行:
execution(public * *(..))
2、任何一个以“set”开始的方法的执行:
execution(* set*(..))
3、AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
4、定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
5、定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
6、定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名
下面是一个基于配置文件的AOP环绕通知
<!--配置第三方平台请求日志切面-->
<bean id="logHandler" class="com.baidu.map.advice.LogHandler" />
<aop:config>
<aop:aspect id="logAspect" ref="logHandler">
<aop:pointcut id="logPointCut" expression="execution(public * com.baidu.map.itf.*Ctr.*Handle(..))"/>
<aop:around method="doAround" pointcut-ref="logPointCut"/>
</aop:aspect>
</aop:config>