1、新建SpringBoot-Test
其中pom.xml 文件如下:
4.0.0
org.springframework.boot
sp服务器托管网ring-boot-starter-parent
2.6.1
org.example
SpringBoot-Test
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.aspectj
aspectjrt
org.aspectj
aspectjweaver
junit
junit
4.12
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中自定义注解的实现过程:
其中代码结构为:
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
MySql存储例程、存储过程进阶学习 什么是存储例程?存储例程是存储在数据库服务器中的一组sql语句,通过在查询中调用一个指定的名称来执行这些sql语句命令。为什么要使用存储过程?我们都知道应用程序分为两种,一种是基于web,一种是基于桌面,他们都和数据库进行…