spring aop两种配备方式
spring aop两种配置方式
<aop:config> <aop:aspect id="SaveBlogAspect" ref="saveLogWithSession"> <aop:pointcut id="SaveBlogPointcut" expression="execution(* com.coo8.bip.*..*.getAllSysCompany(..)) and args(request,..)" /> <aop:after-returning method="saveLog" pointcutref="SaveBlogPointcut" arg-names="request"/> </aop:aspect> </aop:config> <bean id="saveLogWithSession" class="com.coo8.bip.util.SaveLogWithSession"> </bean>
自定义类SaveLogWithSession的saveLog()方法可以得到spring注入的request参数。
因为配置为after-returning方式,所以系统执行完getAllSysCompany()方法之后会执行saveLog()方法,
并把request参数传递给saveLog()方法。前提是getAllSysCompany()方法也要有request参数。
<aop:config> <aop:pointcut id="addManagermethod" expression="execution(* com.coo8.bip.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="addManagermethod" /> <aop:advisor pointcut="execution(* com.coo8.bip.*..*DAO.save*(..)) || execution(* com.coo8.bip.*..*DAO.update*(..)) || execution(* com.coo8.bip.*..*DAO.get*(..)) || execution(* com.coo8.bip.*..*DAO.delete*(..))" advice-ref="logMethodBeforeAdvice"/> </aop:config> <bean id="logMethodBeforeAdvice" class="com.coo8.bip.util.DaoLogMethodBeforeAdvice"> </bean>
这个例子与上例不同之处是没有定after-returning切面类型,所以要通过自定义类
DaoLogMethodBeforeAdvice实现MethodBeforeAdvice、AfterReturningAdvice接口,来告诉spring
在什么时候执行该切面