SpringBoot

  • 一、SpringBoot自定义注解
  • 1、JDK基本注解
  • 2、JDK元注解
  • 2.1、@Retention
  • 2.2、@Target
  • 2.3、@Inherited
  • 2.4、@Documented
  • 二 、自定义注解开发
  • 2.1、@interface关键字
  • 2.2、生成一个注释
  • 2.3、取注解里的属性值
  • 2.4、 判断在该类有无该注解
  • 三 、完成切面日志操作
  • 3.1 、当我们在写增删改的时候,会有很多冗余的代码
  • 3.2、定义aop面向切面
  • 3.3 、注解来开发aop日志


一、SpringBoot自定义注解

  • Java注解是附加在代码中的一些元信息,用于一些工具在编译、 运行时进行解析和使用,起到说明、配置的功能

1、JDK基本注解

@Override ——》重写
	@Deprecated ——》已过时
	@SuppressWarnings(value = "unchecked")  ——》压制编辑器警告

2、JDK元注解

  • 元注解用于修饰其他的注解

2.1、@Retention

  • 定义注解的保留策略
//注解仅存在于源码中,在class字节码文件中不包含
	@Retention(RetentionPolicy.SOURCE) 
	//默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
	@Retention(RetentionPolicy.CLASS)
	//注解会在class字节码文件中存在,在运行时可以通过反射获取到
	@Retention(RetentionPolicy.RUNTIME)

2.2、@Target

  • 指定被修饰的Annotation可以放置的位置(被修饰的目标)
@Target(ElementType.TYPE)  ——》接口、类
      @Target(ElementType.FIELD)   ——》属性
      @Target(ElementType.METHOD)   ——》方法
      @Target(ElementType.PARAMETER)   ——》方法参数
      @Target(ElementType.CONSTRUCTOR)  ——》构造函数
      @Target(ElementType.LOCAL_VARIABLE)  ——》局部变量
      @Target(ElementType.ANNOTATION_TYPE)  ——》注解
      @Target(ElementType.PACKAGE)   ——》包 
      
注:可以指定多个位置
@Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用

2.3、@Inherited

  • 指定被修饰的Annotation将具有继承性

2.4、@Documented

  • 指定被修饰的该Annotation可以被javadoc工具提取成文档

二 、自定义注解开发

2.1、@interface关键字

Annotation的成员变量,在Annotation定义中是以无参的方法形式来声明的。
	其方法名和返回值类型定义了该成员变量的名字和类型。
	我们还可以使用default关键字为这个成员变量设定默认值。

2.2、生成一个注释

package com.lv.annotation;
 
import org.springframework.beans.factory.annotation.Autowired; 
import java.lang.annotation.*; 

// 指定被修饰的该Annotation可以被javadoc工具提取成文档
@Documented
//表示当前注解可以打在什么东西上面,此处可以放在类上与方法上
@Target({ElementType.TYPE,ElementType.METHOD})
//指定被修饰的Annotation将具有继承性
@Inherited
//注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.SOURCE)
public @interface MyAnnotation {
	String message() default "aaa";
}

2.3、取注解里的属性值

public class Test {
    public static void main(String[] args) {
//    反射
        for(Annotation a:TestController.class.getAnnotations()){
            if(a instanceof MyAnnotation){
                System.out.println(((MyAnnotation) a).message());
            }
        }
    }
}
  • MyAnnotation注解为@Retention(RetentionPolicy.RUNTIME)时,注解会在class字节码文件中存在,在运行时可以通过反射获取到
public class Test {
    public static void main(String[] args) {
//    反射
        for(Annotation a:TestController.class.getAnnotations()){
            System.out.println(a);
        }
 
    }

spring boot 自定义注解 实体属性 springboot自定义注解 解析_java

2.4、 判断在该类有无该注解

public class Test {
	    public static void main(String[] args) {
	//        直接将MyAnnotation这注解取出
	        MyAnnotation myAnnotation=TestController.class.getAnnotation(MyAnnotation.class);
	        if(myAnnotation !=null){
	            System.out.println(myAnnotation.message());
	        }
	 
	    }
	}

三 、完成切面日志操作

3.1 、当我们在写增删改的时候,会有很多冗余的代码

@RequestMapping("/add")
    public String add(){
        System.out.println("xxx在增加");
        System.out.println("增加成功");
        return "yes";

3.2、定义aop面向切面

  • 将前面那部分放入前置通知,后面一部分后置通知
package com.lv.aop;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
@Aspect
//类不被识别,将类变成一个组件
@Component
@Slf4j
public class LogAop {
//    指定切入的规则,".."代表可有参可无参
    @Pointcut("execution(* com.lv.controller.*Controller.*(..))")
    public  void logger(){}
 
//    环绕通知
    @Around("logger()")
    public Object around(ProceedingJoinPoint point){
        //        获得方法名称
        Signature methodName=point.getSignature();
//        日志输出
        log.info(methodName+"进来了");
        Long l1=System.currentTimeMillis();
//        让方法执行
        Object obj=null;
        try {
            obj=point.proceed(point.getArgs());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        log.info(methodName+"走了"+"\t耗时"+(System.currentTimeMillis()-l1));
        return obj;
    }
}
package com.lv.controller;
 
import com.lv.annotation.MyAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@MyAnnotation
//直接返回json数据
@RestController
//返回页面跳转数据
//@Controller
public class TestController {
    @RequestMapping("/add")
    public String add(){
        return "yes";
    }
    @RequestMapping("/del")
    public String del(){
        return "yes";
    }
    @RequestMapping("/upd")
    public String upd(){
        return "yes";
    }
    @RequestMapping("/list")
    public String list(){
        return "yes";
    }
}

spring boot 自定义注解 实体属性 springboot自定义注解 解析_System_02

3.3 、注解来开发aop日志

package com.lv.annotation;
 
import java.lang.annotation.*;
 
@Inherited
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
 
}
//同样在切面类中,记得改变切入的规则
//@Pointcut("@annotation(com.lv.annotation.MyLog)")
  • 需要输出日志的方法就将新建的注解加上