spring aop小例子

切面类 package com.holley.coms.web.pub.sys; import org.aspectj.lang.JoinPoint; public class AopTest { public void aopTestPRint(JoinPoint j){ System.out.println("添加日志-----------------------"); Object obj[] = j.getArgs(); for(Object o:obj){ System.out.println(o); } System.out.println("=======方法名:"+j.getSignature().getName());//打印出方法名称 } } **applicationcontext.xml配置文件** //当出现nosuch method.proxy70.某某方法 错误时配置该行 <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="aspect" class="com.holley.coms.web.pub.sys.AopTest" /> <aop:config> <aop:aspect id="myaop" ref="aspect"> <aop:pointcut id="target" expression="execution(* com.holley.coms.pub.pob.service.PobBranchMeterService.*(..))" /> //项目中的service层,这里的配置可以网上去搜索 <aop:before method="aopTestPrint" pointcut-ref="target" /> </aop:aspect> </aop:config> **控制台打印出来的** 添加日志----------------------- {[email protected], TQID=114} =======方法名:queryBranchMeterByPage 跟事务配置在一起的时候: <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="getPage" propagation="REQUIRED" /> <tx:method name="create*" propagation="NOT_SUPPORTED" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.holley.coms.*.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> <aop:aspect id="myaop" ref="aspect"> <aop:pointcut id="target" expression="execution(* com.holley.coms.pub.pob.service.PobBranchMeterService.*(..))" /> <aop:before method="aopTestPrint" pointcut-ref="target" /> </aop:aspect> </aop:config>

以上代码亲测可用