一、添加aop starter依赖
4.0.0
com.chhliu.springboot.aop
springboot-aop
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
1.5.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.0.27
org.springframework.boot
spring-boot-maven-plugin
二、配置文件中开启支持
spring.aop.auto=true
三、编写切面
package com.chhliu.springboot.aop.aop;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.druid.support.json.JSONUtils;
@Component // 注册到Spring容器,必须加入这个注解
@Aspect // 该注解标示该类为切面类,切面是由通知和切点组成的。
public class ApiAspect {
private static final Logger logger = LoggerFactory.getLogger(ApiAspect.class);
ThreadLocal startTime = new ThreadLocal();
@Pointcut("execution(* com.chhliu.springboot.aop.controller.HelloController.*(..))")// 定义切点表达式
@Order(2)
public void controllerPoint() {
}
@Pointcut("@annotation(com.chhliu.springboot.aop.annotation.RedisCache)")// 定义注解类型的切点,只要方法上有该注解,都会匹配
@Order(1)
public void annotationPoint(){
}
// 定义前置通知
@Before("annotationPoint()")
public void before(JoinPoint joinPoint) {
System.out.println("方法执行前执行.....before");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
logger.info("<====================================================================");
logger.info("请求来源: => " + request.getRemoteAddr());
logger.info("请求URL: " + request.getRequestURL().toString());
logger.info("请求方式: " + request.getMethod());
logger.info("响应方法: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("请求参数 : " + Arrays.toString(joinPoint.getArgs()));
logger.info("---------------------------------------------------------------------");
startTime.set(System.currentTimeMillis());
}
@Around("controllerPoint() && args(arg)")// 需要匹配切点表达式,同时需要匹配参数
public String around(ProceedingJoinPoint pjp, String arg) {
System.out.println("name:"+arg);
System.out.println("方法环绕start....around.");
String result = null;
try {
result = (String) pjp.proceed()+" aop String";
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("方法环绕end.....around");
return result;
}
@After("within(com.chhliu.springboot.aop.controller.*Controller)")
public void after() {
System.out.println("方法之后执行....after.");
}
@AfterReturning(pointcut="controllerPoint()", returning="rst")
public void afterReturning(JoinPoint joinPoint, Object rst) {
System.out.println("方法执行完执行.....afterReturning");
logger.info("耗时(毫秒) : " + (System.currentTimeMillis() - startTime.get()));
logger.info("返回数据: {}", JSONUtils.toJSONString(rst));
logger.info("====================================================================>");
}
@AfterThrowing("within(com.chhliu.springboot.aop.controller.*Controller)")
public void afterThrowing() {
System.out.println("异常出现之后.....afterThrowing");
}
}
四、定义注解
package com.chhliu.springboot.aop.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 描述:加入查询结果的缓存
* @author chhliu
* 创建时间:2017年2月14日 下午10:30:00
* @version 1.2.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface RedisCache {
Class> type();//被代理类的全类名,在之后会做为redis hash 的key
}
五、编写Controller
package com.chhliu.springboot.aop.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.chhliu.springboot.aop.annotation.RedisCache;
@RestController
public class HelloController {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name", required = true) String name) {
String result = "hello " + name;
System.out.println(result);
return result;
}
@RequestMapping(value = "/world")
public String world(@RequestParam(value = "arg", required = true) String arg){
String result = "world " + arg;
System.out.println(result);
return result;
}
@RequestMapping(value="/annotation")
@RedisCache(type=String.class)
public String annotation(){
return "annotation";
}
}
六、测试
1、启动服务
2、在浏览器中输入:http://localhost:8080/annotation
3、测试结果如下:
方法执行前执行.....before
2017-02-14 15:23:42.273 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : <====================================================================
2017-02-14 15:23:42.274 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 请求来源: => 127.0.0.1
2017-02-14 15:23:42.274 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 请求URL: http://localhost:8080/annotation
2017-02-14 15:23:42.274 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 请求方式: GET
2017-02-14 15:23:42.276 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 响应方法: com.chhliu.springboot.aop.controller.HelloController.annotation
2017-02-14 15:23:42.276 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 请求参数 : []
2017-02-14 15:23:42.276 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : ---------------------------------------------------------------------
方法之后执行....after.
方法执行完执行.....afterReturning
2017-02-14 15:23:42.286 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 耗时(毫秒) : 10
2017-02-14 15:23:42.289 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : 返回数据: "annotation"
2017-02-14 15:23:42.291 INFO 2208 --- [nio-8080-exec-1] com.chhliu.springboot.aop.aop.ApiAspect : ====================================================================>
由于Around环绕通知不匹配,所以没有切入进去!