1.前言
遇到任务要求:
针对接口封装,所有的接口都要进行封装,并记录接口的调用次数,接口调用的成功与失败error code,以及接口的响应时长
解决办法:
SpringBoot的AOP面向切面编程有五种通知,环绕通知是最为强大的通知,具备了前置通知和后置通知的结合
2.依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Wechat</groupId>
<artifactId>Wechat</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--AOP主要依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<finalName>SpringSession</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.配置类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@org.aspectj.lang.annotation.Aspect
public class Aspect {
//匹配com.wechat.lease.controller包及其子包下的所有类的所有方法
@Pointcut("execution(* com.wechat.lease.controller..*.*(..))")
public void executeService() {
}
/**
* 环绕通知: 环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型
*/
@Around("execution(* com.wechat.lease.controller..*.*(..))")
public Object doAroundAdvice(ProceedingJoinPoint point) {
System.out.println("环绕通知被执行,目标方法执行之前");
try {
Object obj = point.proceed(); //执行方法
System.out.println("环绕通知被执行,目标方法执行之后");
return obj;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
4.controller类和启动类APP
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WechatController {
@RequestMapping(value="/aa", method=RequestMethod.GET)
public String Wechat(){
System.out.println("---------------");
return "开心一刻";
}
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5.测试结果:

指导qq:179061434
demo地址:链接: https://pan.baidu.com/s/1PgM5APWQHOyrFuXeLNh2GQ 密码: v4nj
网友评论