首页 > 其他分享 >注解

注解

时间:2023-09-09 11:58:09浏览次数:27  
标签:System value class println 注解 public

title: 注解
index_img: https://tuchuangs.com/imgs/2023/08/14/ea3a278b72817316.png
tags:
  - Java SE
categories:
  - Java SE
excerpt: 注解

Annotation简述

就是Java代码里的特殊标记,比如:@Override@Test等,让其他程序根据注解信息来决定怎么执行该程序。

注解可以用在类上、构造器上、方法上、成员变量上、参数上、等位置处。【可以自己决定】

自定义注解

public @interface 名字{
  public 属性类型【任意】 属性名 default 默认值;
}
public @interface Simple {
    String name();

    int age() default 0; //有默认值,使用时可以不写

    String[] core();
}
@Simple(name = "java", age = 100, core = {"c", "c++"}) //用在类上
public class Main {
    public static void main(String[] args) {

    }

    @Simple(name = "java", age = 100, core = {"c", "c++"}) //用在方法上
    public static void method() {

    }
}

特殊属性

属性名:value

注解定义了value属性,如果没有其它属性,可以写XXX,否则要写value=xxx

image-20230813231200232

@Simple(name = "java", age = 100, core = {"c", "c++"}) //用在类上
public class Main {
    public static void main(String[] args) {

    }

    @Simple(name = "java", age = 100, core = {"c", "c++"}) //用在方法上
    @end(value = "10",size = 10) //Value必须写
    @Start("1") //相当于 value="1"
    public static void method() {

    }
}

注解的原理

上方Simple反编译

import java.lang.annotation.Annotation;

public interface Simple
	extends Annotation
{

	public abstract String name(); 

	public abstract int age();

	public abstract String[] core();
}
  • 注解的本质是接口,都继承自Annotation接口
  • 注解{….}是接口实现类

元注解

即修饰注解的注解。

image-20230813233819922

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME) //运行阶段
@Target({ElementType.PARAMETER, ElementType.TYPE})  //只能用在类和方法参数
public @interface Simple {
    String value() default "";
}
@Simple
public class Main {
    public void myMethod(@Simple String param) {

    }
}

注解的解析

就是判断类上、方法上、成员变量上是否存在注解,并把注解里的内容给解析出来。

image-20230813234352651

image-20230813234441887

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@interface MyAnnotation1 {
    String value() default "";

    String sex() default "男";
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@interface MyAnnotation2 {
    String value() default "";

    String sex() default "女";
}

// 使用定义的注解
@MyAnnotation1(value = "Class annotation1")
@MyAnnotation2(value = "Class annotation2")
class MyClass {

    @MyAnnotation1(value = "Method annotation1")
    @MyAnnotation2(value = "Method annotation2")
    public void myMethod() {
    }
}

public class Main {
    public static void main(String[] args) throws NoSuchMethodException {
        // 解析类上的指定注解
        MyAnnotation1 classAnnotation1 = MyClass.class.getDeclaredAnnotation(MyAnnotation1.class);
        System.out.println(classAnnotation1); //输出注解
        System.out.println(classAnnotation1.value() + " " + classAnnotation1.sex()); //输出它的值

        MyAnnotation2 classAnnotation2 = MyClass.class.getDeclaredAnnotation(MyAnnotation2.class);
        System.out.println(classAnnotation2);
        System.out.println(classAnnotation2.value() + " " + classAnnotation2.sex());

        System.out.println("---------------");

        // 解析类上的所有注解
        Annotation[] annotations = MyClass.class.getDeclaredAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        System.out.println("----------------");

        //是否有指定注解
        Class<MyClass> myClassClass = MyClass.class;
        if (myClassClass.isAnnotationPresent(MyAnnotation1.class)) {
            System.out.println("存在注解MyAnnotation1在类上");
        } else {
            System.out.println("不存在注解MyAnnotation1在类上");
        }

        System.out.println("---------------");

        // 解析方法上的指定注解
        Method method = MyClass.class.getDeclaredMethod("myMethod");
        MyAnnotation1 methodAnnotation = method.getDeclaredAnnotation(MyAnnotation1.class);
        System.out.println("Method annotation: " + methodAnnotation.value());
    }
}

image-20230814004609619

简易Junit

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {
    String value() default " ";
}

public class FalseJunit {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        FalseJunit falseJunit = new FalseJunit();

        //解析方法,如果有@Test注解,就执行

        Method[] methods = FalseJunit.class.getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(Test.class)) {
                Object invoke = method.invoke(falseJunit);
            }
        }
    }

    @Test
    public void Meth1() {
        System.out.println("自定义@Test注解方法1");
    }

    public void Meth2() {
        System.out.println("方法2");
    }

    @Test
    public void Meth3() {
        System.out.println("自定义@Test注解方法3");
    }

    public void Meth4() {
        System.out.println("方法4");
    }
}

image-20230814004003046

标签:System,value,class,println,注解,public
From: https://www.cnblogs.com/SimpleWord/p/17689198.html

相关文章

  • spring 实现定时任务(手动配置,不用注解)
    1.情景展示在java当中实现定时任务,主要有两种。一种是通过java源码实现,另一种是通过spring框架来实现。由于我们现在基本上使用的都是spring框架(SpringMVC、SpringBoot),况且,使用spring实现定时任务,代码更加简洁。那么,如何是想spring来实现呢?2.具体分析使用spring实现,具体有......
  • SpringSecurity中注解讲解
    目录[email protected]@PreAuthorize1.1.1开启注解1.1.2使用注解原生方法1.1.3使用注解自定义方法[email protected]@Secured2其他注解[email protected]@PreFilter3权限表达式1@EnableGlobalMethodSecurity@EnableGlobalMethodSecurity是Spring......
  • @Override注解
    @Override是一个Java注解(Annotation),它用于表示一个方法(包括构造方法和非静态、非私有的方法)重写(覆盖)了其父类或实现的接口中的方法1。使用@Override注解可以让编译器帮助我们检查方法是否正确地覆盖了父类或接口中的方法,以避免在运行时出现意外情况。如果父类或接口中没有要重写......
  • @Component注解
    @Component是Spring框架中的一种注解,用于标注一个类作为组件。@Component注解可以标注一个类,这个类将会被Spring容器自动创建和管理。使用@Component注解标注的类,可以通过Spring提供的API进行获取和管理,也可以通过@Autowired注解将该类的实例注入到其他类中。除了@Component,Spr......
  • @Documented注解
    @Documented注解是一个标记注解,用于指示将被注解的元素包含在生成的Java文档中1。它本身没有任何属性或配置选项,通常用于自定义注解时,确保其注解信息能在生成的文档中显示1。例如:如果声明注解时指定了@Documented,则它会被javadoc之类的工具处理,所以注解类型信息也会被包括在生成......
  • @Scheduled注解与参数
    基本参数用法@Scheduled由Spring定义,用于将方法设置为调度任务。如:定时执行一次或定时轮询执行一段代码)参数详解1、fixedDelay上一次任务执行完毕时间点之后多长时间再执行,任务的执行要等上一个任务执行结束。@Scheduled(fixedDelay=60*60*1000)  //每隔1小时执行2、fixedRate......
  • Spring注解开发
    在Spring4之后,要使用注解开发,必须保证aop的依赖包导入。这里我们在maven的pom.xml中导入spring-webmvc这个大的依赖整合包就可以了。<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>6.0.11</version></d......
  • java http传参及使用的注解
    javahttp传参及使用的注解:TTP协议组成协议内容示例对应Spring注解pathinfo传参/articles/12(查询id为12的文章,12是参数)@PathVariableURLQueryString传参/articles?id=12@RequestParamBody传参Content-Type:multipart/form-data@RequestParamBod......
  • Spring 参数校验注解失效
    问题描述使用@Notnull、@Max、@Min等参数校验注解时失效。解决在Controller层请求参数前加入@Valid注解//查询用Get@GetMapping("/query-list")//@Valid让req中的验证注解生效publicCommonResp<List<PassengerQueryResp>>queryList(@ValidPasse......
  • 使用注解实现applicationContext.xml中的内容
    2023-09-07packagecom.hh.config;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;/***@authorhh*@version1.0*@DATE2023-09-0712:14:10*/@Configuration@ComponentScan......