系统环境:
操作系统: win10
jdk版本:
openjdk version "12" 2019-03-19 OpenJDK Runtime Environment (build 12+33) OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
idea版本:
IntelliJ IDEA 2020.2.1 (Ultimate Edition) Build #IU-202.6948.69, built on August 25, 2020 Licensed to hello Subscription is active until September 11, 2021 For educational use only. Runtime version: 11.0.8+10-b944.31 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 10 10.0 GC: ParNew, ConcurrentMarkSweep Memory: 966M Cores: 8 Non-Bundled Plugins: Lombook Plugin, com.intellij.kubernetes, training
maven版本:3.6.3
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 https://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>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.rurjs.example</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>14</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
切面类
package com.rurjs.example.eureka.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Aspect @Component public class RjsAspectResponse { @Pointcut("@annotation(com.rurjs.example.eureka.aspect.RjsResponse)") private void makeResponse() { // return "hi"; } /** * 定制一个环绕通知 * @param joinPoint * @return */ @Around("makeResponse()") public Object advice(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Around Begin"); Object res = (String) joinPoint.proceed();//执行到这里开始走进来的方法体(必须声明) System.out.println("Around End"); return res; } //当想获得注解里面的属性,可以直接注入该注解 //方法可以带参数,可以同时设置多个方法用&& @Before("makeResponse()") public void record(JoinPoint joinPoint) { System.out.println("Before"); } @After("makeResponse()") public void after() { System.out.println("After"); } }
注解类
package com.rurjs.example.eureka.aspect; import org.springframework.web.bind.annotation.Mapping; import java.lang.annotation.*; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RjsResponse { String value() default ""; }