首页

通过Spring以注释的方式(Aspect、Pointcut、Before、Around及After)实现AOP面向切面编程代码示例

标签:面向切面,Aspect,ProceedingJoinPoint,annotation,注释,spring,Pointcut,Before,Around,After,拦截器     发布时间:2017-06-17   

一、前言

通过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的申明,并添加<aop:aspectj-autoproxy proxy-target-class="true"/> 配置项

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@		xmlns:context="http://www.springframework.org/schema/context"@b@		xmlns:aop="http://www.springframework.org/schema/aop"@b@		xsi:schemaLocation="@b@			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd@b@			http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd@b@			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">@b@		@b@	<!-- 自动扫描Action加入Spring上下文 Scope:prototype -->@b@	<context:component-scan base-package="com.xwood.test" scope-resolver="org.springframework.context.annotation.Jsr330ScopeMetadataResolver"/>@b@	<aop:aspectj-autoproxy proxy-target-class="true"/> @b@		@b@</beans>

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层逻辑已经执行完成