c语言around用法,通过Spring以注释的方式(Aspect、Pointcut、Before、Around及After)实现AOP面向切面编程代码示例...

一、前言

通过Spring注释的方式(基于xml配置文件的方式参考其他页)实现面向切面编程AOP的,首先定义切面方法如@Pointcut("execution(public * com.xwood.test.action.*Action.*(..))"),然后再通过@Before、@Around、@After注释在方法前后处理公共的业务逻辑,如日志、通用抽象逻辑处理等

二、代码示例

1. 定义日志处理拦截器LogIntercept,具体代码如下import org.aspectj.lang.ProceedingJoinPoint;@b@import org.aspectj.lang.annotation.After;@b@import org.aspectj.lang.annotation.Around;@b@import org.aspectj.lang.annotation.Aspect;@b@import org.aspectj.lang.annotation.Before;@b@import org.aspectj.lang.annotation.Pointcut;@b@import org.springframework.stereotype.Component;@b@@b@@Aspect@b@@Component@b@public class LogIntercept{@b@     @b@    @Pointcut("execution(public * com.xwood.test.action.*Action.*(..))")@b@    public void actionLog(){}@b@     @b@     @b@    @Before("actionLog()")@b@    public void before() {@b@        this.printLog(" @Before  actionLog() 准备打印action层日志...  ");@b@    }@b@     @b@    @Around("actionLog()")@b@    public void around(ProceedingJoinPoint pjp) throws Throwable{@b@        this.printLog(" @Around  actionLog() 准备打印action层日志... ");@b@        pjp.proceed();@b@        this.printLog(" @Around  actionLog() action层逻辑已经执行完成  ");@b@    }@b@     @b@     @b@    @After("actionLog()")@b@    public void after() {@b@        this.printLog(" @After  actionLog() action层逻辑已经执行完成  ");@b@    }@b@     @b@     @b@    private void printLog(String str){@b@        System.out.println(str);@b@    }@b@    @b@}

2. 定义Action层的用例如UserAction,如下import org.springframework.stereotype.Controller;@b@@b@@Controller@b@public class UserAction{@b@@b@public void   login(){@b@        System.out.println("  【UserAction】 用户登录 ... ");@b@    }@b@@b@}

3.spring的相关配置,定义xmlns:aop="http://www.springframework.org/schema/aop"、http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd的申明,并添加 配置项<?xml  version="1.0" encoding="UTF-8"?>@b@@b@@b@@b@@b@ @b@@b@

4. 测试用例类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@}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值