首页 > 学院 > 开发设计 > 正文

spring aop小例子

2019-11-06 06:39:06
字体:
来源:转载
供稿:网友
切面类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>**控制台打印出来的**添加日志-----------------------{page=com.holley.eemas.common.dataobject.Page@1e7d956, 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>

以上代码亲测可用


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表