1.pom.xml 引入依赖包,并创建启动类
<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>
<!--引入父标签 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<groupId>cn.moye</groupId>
<artifactId>spring-boot-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-aop</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- web 依赖 -->
<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>
<!-- 热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<!-- maven插件 -->
<properties>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>spring-boot-aop</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<srouce>${jdk.version}</srouce>
<target>${jdk.version}</target>
<encoed>${project.build.sourceEncoding}</encoed>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.编写切面测试类
package cn.moye.springboot.aspect;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
/**
* 定义切面
*
*/
@Aspect
@Configuration
public class LoggerAopConfig {
private static Log logger = LogFactory.getLog(LoggerAopConfig.class);
/**
* 定义切入点:拦截cn.moye.springboot.controller包以及子包下的所有类
*/
@Pointcut("execution(* cn.moye.springboot.controller..*.*(..))")
public void executeSystem() {
}
/**
* 前置通知
* @param point 目标类连接点对象
*/
@Before("executeSystem()")
public void invokeBefore(JoinPoint point) {
logger.info("调用------" + getClassName(point) + " 执行 " + getMethodName(point) + " 方法之前");
}
/**
* 后置通知
* @param point 目标类连接点对象
*/
@After("executeSystem()")
public void invokeAfter(JoinPoint point) {
logger.info("调用------" + getClassName(point) + " 执行 " + getMethodName(point) + " 方法之后");
}
/**
* 获取被代理对象的类全名
*
* @param point
* @return
*/
private String getClassName(JoinPoint point) {
return point.getTarget().getClass().getName();
}
/**
* 获取方法名
*
* @param point
* @return
*/
private String getMethodName(JoinPoint point) {
return point.getSignature().getName();
}
}
3.编写控制器测试类
package cn.moye.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* controller测试类
*/
@RestController
public class AopController {
@RequestMapping("/aop")
public String testAop() {
return "test Aop";
}
@RequestMapping("/test")
public String testAop1(){
return "Aop Test";
}
}
4.测试结果
2017-09-29 10:24:29.480 INFO 3984 --- [nio-8080-exec-1] c.m.springboot.aspect.LoggerAopConfig : 调用------cn.moye.springboot.controller.AopController 执行 testAop 方法之前
2017-09-29 10:24:29.488 INFO 3984 --- [nio-8080-exec-1] c.m.springboot.aspect.LoggerAopConfig : 调用------cn.moye.springboot.controller.AopController 执行 testAop 方法之后