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>
**控制台打印出来的**

添加日志-----------------------
{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>

以上代码亲测可用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值