java aop xml配置_通过Spring的xml配置文件实现AOP的面向切面编程完整demo代码示例...

一、前言

通过在Spring的xml配置文件(基于注释的方式可参考其他页)对aop:config、aop:aspect、aop:pointcut、aop:before、aop:after、aop:after-returning及aop:after-throwing项分别进行配置,实现基于AOP面向切面方式记录日志功能的代码示例

二、代码示例

1.记录日志主类LogIntercept拦截器,代码如下public class LogIntercept{@b@     @b@    public void actionLog(){}@b@     @b@     @b@    public void before() {@b@        this.printLog(" @Before  actionLog() 准备打印action层日志...  ");@b@    }@b@     @b@    public void after() {@b@        this.printLog(" @After  actionLog() action层逻辑已经执行完成  ");@b@    }@b@    @b@    public void afterReturn() throws Throwable{@b@         this.printLog(" @afterReturn  actionLog()  end ......  ");@b@    }@b@    @b@    public void afterThrow() {@b@        this.printLog(" @afterThrow  actionLog() action层逻辑已经执行完成  ");@b@    }@b@     @b@     @b@    private void printLog(String str){@b@        System.out.println(str);@b@    }@b@    @b@}

2.spring配置文件<?xml  version="1.0" encoding="UTF-8"?>@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@@b@

3.测试用例代码ActionLogTest,代码如下import org.junit.Test;@b@import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.test.context.ContextConfiguration;@b@import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;@b@@b@import com.xwood.test.action.UserAction;@b@@b@@ContextConfiguration(locations={"classpath*:spring-*.xml"})@b@public class ActionLogTest extends  AbstractJUnit4SpringContextTests {@b@@b@@Autowired@b@private  UserAction action;@b@@b@@Test@b@    public  void   test() throws Exception{@b@action.login();@b@    }@b@@b@}

控制台输出日志@Before  actionLog() 准备打印action层日志...  @b@  【UserAction】 用户登录 ... @b@ @After  actionLog() action层逻辑已经执行完成  @b@ @afterReturn  actionLog()  end ......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值