Spring Boot教程
SpringBoot使用AOP实现统一日志管理:
1、maven pom.xml
<?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>org.example</groupId> <artifactId>spring-boot-example</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、AOP定义,包括切面,切入点、通知
package com.example.common; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LogAop { @Before("execution(public * com.example.boot..*Controller..*(..))") public void log(JoinPoint joinPoint) { String className = joinPoint.getSignature().getDeclaringTypeName(); String name = joinPoint.getSignature().getName(); System.out.println("log before ..." + className + "." + name); } }
3、Controller
package com.example.boot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") @Api("用户API") public class UserController { @RequestMapping(value = "/get/{id}", method = RequestMethod.GET) @ApiImplicitParam(name = "id", value = "UserId", paramType = "path", required = true, defaultValue = "1", dataTypeClass = Integer.class) public String getUser(@PathVariable("id") Integer id) { return "hello:"+id; } }
4、打开AOP开关
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @MapperScan("com.example.boot") @SpringBootApplication(scanBasePackages = {"com.example.boot", "com.example.common"}) @EnableAspectJAutoProxy(proxyTargetClass = true) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5、测试
http://localhost:8080/user/get/1
结果打印:
log before ...com.example.boot.UserController.getUser
hello:1