Java 注解浅析

前言

    前段时间一直想了解下java的自定义注解,包括面试的时候也遇到过类似问题,今天总算有时间来学习下。

正文

注解(Annotation)概念

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

注解和注释的不同

  • 注释
         注释是对代码的说明,给代码的读者看,便于帮读者梳理业务逻辑;
  • 注解
        注解也是对代码的说明,需要配合工具(解析它的代码)使用,参与代码的编译,给应用程序看的;

为什么出现注解?

     在频繁使用注解之前,是用xml 来作为元数据使用,在最开始使用ssm(Spring,SpringMVC,Mybatis)框架时,bean与bean之间的依赖关系是通过xml文件来配置,这个xml文件和源代码分散,当微服务、分布式注解流行,系统越来越大,需要越来越多的xml文件来配置管理,文件配置冗长,而且类型不安全(运行期才会发现错误),此时就需要一种比较简单直观而且类型安全的配置方式,注解就应运而生了,按照Springboot“约定大于配置”的方式,通过注解来约定其含义,更是减少了xml配置文件的数量。
     对于一些经常变动或者配置复杂的配置,使用xml文件来说是比较合适的,所以现在经常是注解和xml方式共存。

注解分类

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

  • JDK注解

    • Java 内置三大注解
      @Override (标记重写方法)
      @Deprecated (标记过时)
      @SuppressWarnings (忽略警告)
    • 元注解 (注解的注解)
      @Target (注解的作用目标)
      @Retention (注解的生命周期)
      @Document (注解是否被包含在JavaDoc中)
      @Inherited (是否允许子类集成该注解)
  • 第三方注解(主要来源于各种框架)

    • Spring注解 @RequestMapping,@RestController,@Configuration,@Value,@Controller,@Service,@Repository,@Component等
    • SpringBoot注解
      @SpringBootApplication,@EnableAutoConfiguration等
    • JPA注解
      @Table,@Entity,@Column,@Id等
      ……
  • 自定义注解
    使用元注解自己定义的注解(下一篇文章将写到实现自定义注解)

什么是注解?

     在程序代码中经常看到的以@ 开头的大部分是注解;

注解定义的格式

修饰符 @interface 注解名 {
    注解元素的声明1 
    注解元素的声明2
}

     注解的元素声明的两种形式

 type elementName();
 type elementName() default value;  

注解定义实例

    我们来看下@Service 注解

// ElementType.TYPE 代表在注解上使用
@Target({ElementType.TYPE})
// RetentionPolicy.RUNTIME 代表运行时使用,可以通过反射获取到
@Retention(RetentionPolicy.RUNTIME)
//包含在JavaDoc中
@Documented
//允许通过包扫描的方式自动检测
@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 注解

package java.lang.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&trade; 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 {
}
 # The common interface extended by all annotation types.Also note that this interface does not itself define an annotation type.
 # 所有的注解默认继承了Annotation接口,但是它本身不能定义注解。

    也就是说,我们见到的所有注解默认是一个实现了Annotation接口的接口。

参考文章

https://segmentfault.com/a/1190000018209300
https://blog.csdn.net/qq_36762677/article/details/82469741

总结

    在这个随处可见注解的时代,还是要了解下注解的,感谢您的阅读~~

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的大白啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值