温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java教程:JAVA自定义注解

发布时间:2020-08-11 17:45:32 来源:ITPUB博客 阅读:149 作者:千锋Python唐小强 栏目:编程语言

注解概念

注解是Java SE 5.0版本开始引入的概念,它是对java源代码的说明,是一种元数据(描述数据的数据)。

注解和注释的不同

  • 注释注释是对代码的说明,给代码的读者看,便于帮读者梳理业务逻辑;在程序代码中经常看到的以@ 开头的大部分是注解;
  • 注解注解也是对代码的说明,需要配合工具(解析它的代码)使用,参与代码的编译,给应用程序看的;

注解分类

注解以@开头,我们会在应用程序中见到各种各样的注解,比如 @Autowired,@Service,@Controller,@Override,@Test,@Value 等等,按照来源划分,可以分为  JDK的注解第三方的注解自定义注解

JDK注解

JAVA 内置注解

  • @Override (标记重写方法)
  • @Deprecated (标记过时)
  • @SuppressWarnings (忽略警告)
元注解 (注解的注解)
  • @Target (注解的作用目标)
  • @Retention (注解的生命周期)
  • @Document (注解是否被包含在JavaDoc中)
  • @Inherited (是否允许子类集成该注解)

第三方注解(各种框架注解)

请自行百度各个框架的注解详解

自定义注解

使用元注解自己定义的注解

JDK中有一些元注解,主要有@Target,@Retention,@Document,@Inherited用来修饰注解。

@Target

表明该注解可以应用的java元素类型

Java教程:JAVA自定义注解
@Retention

表明该注解的生命周期

Java教程:JAVA自定义注解
@Document

表明该注解标记的元素可以被Javadoc 或类似的工具文档化

@Inherited

表明使用了@Inherited注解的注解,所标记的类的子类也会拥有这个注解

注解格式


/**

* 修饰符 @interface 注解名 {
* 注解元素的声明1
* 注解元素的声明2
* }
* 修饰符:访问修饰符必须为public,不写默认为pubic;
* 关键字:必须为 @interface
* 注解名: 注解名称为自定义注解的名称,使用时还会用到;
* 注解类型元素:注解类型元素是注解中内容,可以理解成自定义接口的实现部分;
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
    /**
    * 注解的元素声明的两种形式
    * type elementName();
    * type elementName() default value;  
    */
   String value() default "test";
}

注解样例

接下来我们以Spring中的 @Service 注解为例


@Target({ElementType
.TYPE})
// ElementType.TYPE 代表在注解上使用

@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表运行时使用,可以通过反射获取到
@Documented//包含在JavaDoc中
@Component//允许通过包扫描的方式自动检测
public @interface Service {

/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}

@Annotation

JDK1.5有的,在rt.jar包下 java.lang.annotation包下,所有的注解默认继承了Annotation接口,但是它本身不能定义注解。

package java.lang.annotation;


/**
* 所有的注解默认继承了Annotation接口,但是它本身不能定义注解。
* The common interface extended by all annotation types.  Note that an
* interface that manually extends this one does <i>not</i> define
* an annotation type.  Also note that this interface does not itself
* define an annotation type.
*
* More information about annotation types can be found in section 9.6 of
* <cite>The Java™ Language Specification</cite>.
*
* The { @link java.lang.reflect.AnnotatedElement} interface discusses
* compatibility concerns when evolving an annotation type from being
* non-repeatable to being repeatable.
*
* @author  Josh Bloch
* @since   1.5
*/
public interface Annotation {
   .
   .
   .
}

实现自定义注解

第一步-定义自定义注解


@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
   String value() default "test";
}

第二步-配置注解


@Data

@Builder
@MyTestAnnotation
public class MyBean {
    private String name;
    private int age;
}

第三步-利用反射解析注解


public 

class 
MyTest {


    //isAnnotationPresent:判断当前元素是否被指定注解修饰
   //getAnnotation:返回指定的注解
   //getAnnotations:返回所有的注解
   public static void main(String[] args) {
       try {
           //获取MyBean的Class对象
           MyBean myBean = MyBean.builder().build();
           Class clazz = myBean.getClass();
           
           //判断myBean对象上是否有MyTestAnnotation注解
           if (clazz.isAnnotationPresent(MyTestAnnotation.class)) {
               System.out.println("MyBean类上配置了MyTestAnnotation注解!");
               //获取该对象上MyTestAnnotation类型的注解
               MyTestAnnotation myTestAnnotation = (MyTestAnnotation) clazz.getAnnotation(MyTestAnnotation.class);
               System.out.println(myTestAnnotation.value());
           } else {
               System.out.println("MyBean类上没有配置MyTestAnnotation注解!");
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}

执行main方法,运行结果:

Connected to the target VM, address: 
'127.0.0.1:62125', transport: 
'socket'

MyBean类上配置了MyTestAnnotation注解!
test
Disconnected from the target VM, address: '127.0.0.1:62125', transport: 'socket'
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI