参考地址:http://www.cnblogs.com/parryyang/p/5881523.html
1、基于AspectJ注解
Maven引入依赖包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.1</version> </dependency>
2、在spring配置文件中添加aop的命名空间,同时配置使得项目支持Aop,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--使Spring支持自动检测组件,如注解的Controller --> <context:component-scan base-package="com.parry.redisCluster.*" /> <!--*************** 支持aop **************** --> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
3、拦截代码
package com.wanrong.distribution; import java.util.Arrays; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class UserTokenInterceptor { // com.wanrong.distribution.server.impl 包的子包里面任何方法 @Pointcut("execution(public * com.wanrong.distribution.server.impl.*.*(..))") public void checkToken() { } @Before("checkToken()") public void beforeCheckToken() { System.out.println("调用方法之前。。。。"); } @AfterReturning("checkToken()") public void afterCheckToken() { System.out.println("调用方法结束之后。。。。"); } @Around(value="checkToken()") public Object aroundMethod(ProceedingJoinPoint point){ Object result = null; String methodName = point.getSignature().getName(); try { //前置通知 System.out.println("The method "+ methodName+" start. param<"+ Arrays.asList(point.getArgs())+">"); //执行目标方法 result = point.proceed(); //返回通知 System.out.println("The method "+ methodName+" end. result<"+ result+">"); } catch (Throwable e) { //异常通知 System.out.println("this method "+methodName+" end.ex message<"+e+">"); throw new RuntimeException(e); } //后置通知 System.out.println("The method "+ methodName+" end."); return result; } // 抛出异常时才调用 @AfterThrowing("checkToken()") public void afterThrowing() { System.out.println("校验token出现异常了......"); } }