代码编织梦想

@interface 和 interface 这两个是不一样的

public @interface xxx 定义注解类

public interface xxx 定义接口

自定义注解也就三件事

1、定义注解

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Test {
}

注:@Target 表示这个注解的作用目标

  @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) ///包

2、加注解到方法上,也可能是类上,变量上

  public class Lx { 

     @Test 
     public static void l1() { } 

      public static void l2() { } 

     @Test 
     public static void l3() { 
       throw new RuntimeException("Liu"); 
    } 

    public static void l4() { } 

    @Test 
    public static void l5() { } 

    public static void l6() { } 

    @Test
     public static void l7() { 
       throw new RuntimeException("Xiao"); 
    } 
    public static void l8() { } 
  } 2

3、使用注解

这就涉及到反射的东西,可以去查一下java反射机制

会写注解,会用反射调注解

  public class RunTest { 

    public static void main(String[] args) throws Exception { 

      int passed = 0, failed = 0; 

        for (Method m : Class.forName(args[0]).getMethods()) { 

          if (m.isAnnotationPresent(Test.class)) { //if(方法有@Test注解)

            try { 

              m.invoke(null); 

              passed++; 

            } catch (Throwable ex) { //方法里抛出异常

              System.out.printf("Test %s failed: %s %n", m, ex.getCause()); 

            failed++; 

            } 

          } 

     } 

     System.out.printf("Passed: %d, Failed %d%n", passed, failed); 

   } 

  } 
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_53660431/article/details/128819115

使用自定义注解方式配合spring读取配置文件_宇默的博客-爱代码爱编程_自定义注解读取配置文件中的内容

一、使用@interface关键字来标识为注解类: @Target({ ElementType.TYPE })@Retention( RetentionPolicy.RUNTIME )@Componentpublic @interface Config { } @Target({ ElementType.TYPE })为注解的作用目标  这里表示的是作用为

public @interface xxx 定义注解_鸿蒙道的博客-爱代码爱编程

@interface 不是interface,是注解类 是jdk1.5之后加入的,java没有给它新的关键字,所以就用@interface 这么个东西表示了 这个注解类,就是定义一个可用的注解,包括这个注解用于什么地方,是类,还是方法,还是property,还是方法入参等等 @Retention(RetentionPolicy.RUNTIME) 

自定义注解 public @interface xxx_杨三岁.的博客-爱代码爱编程

自定义注解  public @interface xxx 版权声明:本文为博主原创文章,未经博主允许不得转载,转载请声明博客处出。  今天在看项目时,看到一种新的注解方式,记录下来。 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Tes

spring boot 自定义@enablexxx注解_雷x峰的博客-爱代码爱编程_springboot自定义enable注解

前言 spring boot 自带了很多@EnableXXX这样的注解,通过这些注解我们可以很方便地启用某些功能,比如@EnableAutoConfiguration用来开启自动装配的功能。内部实现主要是通过@Import

自定义springboot @enablexxx注解_路人丁.的博客-爱代码爱编程_自定义springboot里的@enable

上一篇大概讲了springboot 的@EnableAutoConfiguration的实现,这篇我们自定义一个@EnableMyCache注解实现。 1.利用@Import注解加载不在包扫描范围的类,并将其注册到容器。 2.我们知道将Bean注册到IOC容器有很多种方式,从最初的使用XML配置文件的<bean/>标签,到使用注解@Comp

springboot 注解类里面public @interface xxx 什么意思_1466028300的博客-爱代码爱编程

@interface 不是interface,是注解类  定义注解 是jdk1.5之后加入的,java没有给它新的关键字,所以就用@interface 这么个东西表示了  这个注解类,就是定义一个可用的注解,包括这个注解用于什么地方,是类,还是方法,还是property,还是方法入参等等 @Inherited       //这个Annotation 可

Spring Boot 自定义@Enable* 注解-爱代码爱编程

Spring Boot 自定义@Enable* 注解 @Enable*的实例@EnableAsync通过实现ImportSelector接口来实现bean的加载通过实现ImportBeanDefinitionRegistrar接口来实现bean的加载自定义Enable注解来实现对类加载的监听 @Enable*的实例@EnableAsync 在

自定义注解模拟自动注入@Autowired-爱代码爱编程

@Autowired 这种注入属性的方式就是利用了java的反射知识,field.set(value,targetObject); @Autowired 这种注入的方式是setter注入方式的一种变体 自定义注解 LZF // 字段、枚举的常量 @Target(ElementType.FIELD) // 注解会在class字节码文件中存在,在运行时可以

spring boot2.x 如何巧妙的自定义@EnableXXX模块装配功能-爱代码爱编程

一、前言 在平时使用spring boot的时候,很多时候都会用到@EnableXXX的注解,来装配一些功能模块,有代表性的,比如: @EnableWebMvc 开启Web MVC的配置支持;@EnableCaching 开启注解式的缓存支持。如果,想自定义实现这些功能,我们应该怎么做呢?先看一下spring boot是怎么帮我们做的。 @Enabl

@Valid使用详解以及自定义注解的简单使用-爱代码爱编程

@Valid注解用于校验 一.@Valid注解的基本使用: 步骤1:将@Valid注解直接加在变量user之前,并且传入BindingResult对象(也可不传入),用于获取校验失败情况下的反馈信息,如下代码 @RestController @RequestMapping("/user") public class UserController {

自定义@EnableXXX注解-爱代码爱编程

自定义@EnableXXX注解 场景 自定义注解 + AOP 实现接口日志的自动记录(日志记录器),将本项目打包到别的项目上使用时,一开始没有效果,后来发现是因为SpringBoot项目默认注册主程序所在包以及子包的Bean , 而导入的日志记录器里面的所有Bean都没有注入Spring IOC容器 , 所以在主程序上使用@ComponentScan(

Springboot 注解类里面public @interface xxx-爱代码爱编程

———————————————— 原文链接:https://blog.csdn.net/qq_35972907/article/details/97753257 ———————————————— @interface 不是interface,是注解类 定义注解 是jdk1.5之后加入的,java没有给它新的关键字,所以就用@interface 这么个东西

public @interface xxx 自定义注解用法_heikepengmu的博客-爱代码爱编程

自定义注解 public @interface xxx 一种新的注解方式 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @inte

支持spel表达式的自定义日志注解@syslog_@syslog注解_小p同学90的博客-爱代码爱编程

目录 序言预期思路过程结果 序言 之前封装过一个日志注解,打印方法执行信息,功能较为单一不够灵活,近来兴趣来了,想重构下,使其支持表达式语法,以应对灵活的日志打印需求。 该注解是方法层面的日志打印,

自定义注解_如何自定义注解-爱代码爱编程

一、如何自定义注解实现功能 创建自定义注解和创建一个接口相似,但是注解的 interface 关键字需要以 @ 符号开头。 注解方法不能带有参数; 注解方法返回值类型限定为:基本类型、String、Enums、Annotat