1、新建SpringBoot-Test
其中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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>SpringBoot-Test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2、设置启动端口:
3、自定义注解 MyAnotion
package com.springboot.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)//注解实现位置
@Retention(RetentionPolicy.RUNTIME)//元注解 运行时生效
@Documented
@Inherited
public @interface MyAnotion {
String operation() default "";
}
4、自定义切面 MyAnnotationAspect
package com.springboot.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
@Aspect //声明为切面类
@Component //扫描到容器中
public class MyAnnotationAspect {
@Around(value = "@annotation(com.springboot.annotation.MyAnotion)")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
//方法参数
Object[] joinPointArgs = joinPoint.getArgs();
//方法名称 首先获取签名
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
String methodName = method.toString();
//获取方法上的注解
//获取ip地址
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String ip = request.getRemoteAddr();
//获取方法执行时间
System.out.println("=======请求前=======");
//执行方法
joinPoint.proceed();
System.out.println("=======请求后=======");
return null;
}
}
5、编写测试TestController,加上自定义注解 MyAnotion
package com.springboot.controller;
import com.springboot.annotation.MyAnotion;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "test")
public class TestController {
@Test
@MyAnotion
@RequestMapping(value = "/demo")
public void Demo(){
System.out.println("请求成功!");
}
}
6、编写测试接口 MyTestHttp.http
7、验证测试结果:
上边就是简单的SpringBoot中自定义注解的实现过程:
其中代码结构为: