SpringBoot自定义注解

Java注解简介

Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。 注解相关类都包含在java.lang.annotation包中。

Java注解分类:JDK基本注解、JDK元注解、自定义注解

JDK基本注解:

@Override
重写

@SuppressWarnings(value = "unchecked")
压制编辑器警告

JDK元注解  

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

@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}),也就是此注解可以在方法和类上面使用

@Inherited:指定被修饰的Annotation将具有继承性

@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.

自定义注解

注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):

标记Annotation:
没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息

元数据Annotation:
包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;

二、自定义注解应用 

如何自定义注解?

使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:
   Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型,
   而且我们还可以使用default关键字为这个成员变量设定默认值;

Myannotation1 :

package com.zjy.ssm.annotation;
 
import java.lang.annotation.*;
 
/**
 * @author 小猪
 * @create 2022-10-27 11:41
 * 注解可以用在类、接口、属性、方法上
 *  注解运行期也保留
 *  不可继承
 */
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Myannotation1 {
//    指的是注解中的属性
    public String desc() default "desc可以修饰类、方法、属性";
 
    public String value() default "value可以修饰类、方法、属性";
 
 
}

StudentContoller:

package com.zjy.ssm.annotation;
 
/**
 * @author xiaozhu
 * @create 2022-10-27 14:25
 */
@Myannotation1(desc = "标记在类上面")
public class StudentController {
 
    @Myannotation1("标记在属性id上面")
    private String id;
    @Myannotation1("标记在属性name上面")
    private String name;
    @Myannotation1
    public void test1(@Myannotation2("用来修饰id参数") String id,@Myannotation2("用来修饰name参数") String name){
        System.out.println("测试...");
    }
 
}

Myannotation2 :

package com.zjy.ssm.annotation;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * @author xiaozhu
 * @create 2022-10-27 11:41
 * 注解可以用在类、接口、属性、方法上
 *  注解运行期也保留
 *  不可继承
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Myannotation2 {
//    指的是注解中的属性
    public String desc() default "desc可以修饰类、方法、属性";
 
    public String value() default "value可以修饰类、方法、属性";
 
 
}

 

 

Demo1 :

如何获取注解上的类、如何获取注解上的方法、如何获取注解上的属性、如何获取注解上的参数

package com.zjy.ssm.annotation.demo;
 
import com.zjy.ssm.annotation.Myannotation1;
import com.zjy.ssm.annotation.Myannotation2;
import com.zjy.ssm.annotation.StudentController;
 
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
 
/**
 * @author 小猪
 * @create 2022-10-27 15:14
 *
 * 目标:
 * 1、获取studentController 类上自定义注解中的内容
 * 2、获取studentController 方法上自定义注解中的内容
 * 3、获取studentController 属性上自定义注解中的内容
 * 4、获取studentController 参数上自定义注解中的内容
 *
 * * *..*Service.*pager(...)
 *
 */
public class Demo1 {
    public static void main(String[] args) throws Exception {
        Myannotation1 annotation = StudentController.class.getAnnotation(Myannotation1.class);
        System.out.println(annotation.value());
        System.out.println(annotation.desc());
 
//        获取属性上的
        Field id = StudentController.class.getDeclaredField("id");
        Field name = StudentController.class.getDeclaredField("name");
//        Myannotation2 annotation2 = id.getAnnotation(Myannotation2.class);
        System.out.println(id.getAnnotation(Myannotation1.class).value());
        System.out.println(name.getAnnotation(Myannotation1.class).value());
 
        //获取方法上的
        Method m1 = StudentController.class.getDeclaredMethod("test1", String.class, String.class);
        System.out.println(m1.getAnnotation(Myannotation1.class).value());
 
//        获取参数上的标识
        for (Parameter p:m1.getParameters()){
            System.out.println(p.getAnnotation(Myannotation2.class).value());
 
        }
 
 
//        Field[] declaredFields = StudentController.class.getDeclaredFields();
//        for (Field f : declaredFields){
//            Annotation annotation1=f.getAnnotation(Myannotation1.class);
//            if (annotation !=null){
//
//            }
//        }
 
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot中可以自定义注解来实现特定的功能。自定义注解的步骤如下: 1. 使用`@interface`关键字来定义注解,可以在注解中设置属性。 2. 可以通过注解的属性来传递参数,比如设置注解中的属性值。 3. 可以通过判断某个类是否有特定注解来进行相应的操作。 在SpringBoot中,自定义注解可以用于实现日志记录、定时器等功能。通过使用注解,可以简化代码,并提高开发效率。同时,自定义注解也是Spring框架中广泛应用的一种方式,可以在SpringMVC框架中使用注解来配置各种功能。而在SpringBoot框架中,更是将注解的使用推向了极致,几乎将传统的XML配置都替换为了注解。因此,对于SpringBoot来说,自定义注解是非常重要的一部分。123 #### 引用[.reference_title] - *1* *3* [springboot 自定义注解(含源码)](https://blog.csdn.net/yb546822612/article/details/88116654)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* [SpringBoot-自定义注解](https://blog.csdn.net/weixin_44809337/article/details/124366325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值