Reference address: http://www.cnblogs.com/parryyang/p/5881523.html
1. Based on AspectJ annotations
Maven introduces dependency packages
<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. Add the namespace of aop to the spring configuration file, and configure the project to support Aop. The configuration is as follows:
<?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"> <!--Enable Spring to support automatic detection of components, such as annotated Controller --> <context:component-scan base-package="com.parry.redisCluster.*" /> <!--************** support aop **************** --> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
3. Intercept code
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 { // any method in a subpackage of the com.wanrong.distribution.server.impl package @Pointcut("execution(public * com.wanrong.distribution.server.impl.*.*(..))") public void checkToken() { } @Before("checkToken()") public void beforeCheckToken() { System.out.println("Before calling the method..."); } @AfterReturning("checkToken()") public void afterCheckToken() { System.out.println("After calling the method..."); } @Around(value="checkToken()") public Object aroundMethod(ProceedingJoinPoint point){ Object result = null; String methodName = point.getSignature().getName(); try { //pre-notification System.out.println("The method "+ methodName+" start. param<"+ Arrays.asList(point.getArgs())+">"); //Execute the target method result = point.proceed(); // return notification System.out.println("The method "+ methodName+" end. result<"+ result+">"); } catch (Throwable e) { //exception notification System.out.println("this method "+methodName+" end.ex message<"+e+">"); throw new RuntimeException(e); } // post notification System.out.println("The method "+ methodName+" end."); return result; } // Called only when an exception is thrown @AfterThrowing("checkToken()") public void afterThrowing() { System.out.println("There is an abnormality in the verification token..."); } }