历史
注解是JDK1.5引入的一个特性:
JDK 1.5 has a new interesting feature called annotations. It allows you to specify xdoclet like tags right in the Java language just as you can with C#. These tags are typesafe and access to them is available at compile time, load-time, and run-time. See JSR-175 for more detail. JBossAOP since beta2 does support JDK1.5 annotations
我们到JSR-175那里再看一下介绍:
A metadata facility for the JavaTM Programming Language would allow classes, interfaces, fields, and methods to be marked as having particular attributes.
这一年是2004年。
举例说明,在子类的重写方法前使用@Override注解:
class Cat extends Animal {
@Override
public String getFirstName() {
}
}
这样,如果父类更改了getFirstName方法,子类的编译就会报错,这样我们第一时间就会发现,而不是运行报错后再排查。
Spring注解
而Spring Framework 1.0是在2003-2004年间,即处在JDK 1.4到JDK 1.5的过渡时代。其实从Spring Framework 1.2就开始兼容JDK 1.5注解方面的特性,Spring Framework 3.0开始大量出现注解的使用,如条件注解等。
Spring核心注解场景分类
Spring注解 | 场景说明 | 起始版本 |
@Repository | 数据仓储模式注解 | 2.0 |
@Component | 通用组件模式注解 | 2.5 |
@Serive | 服务模式注解 | 2.5 |
@Controller | Web控制器模式注解 | 2.5 |
@Configuration | 配置类模式注解 | 3.0 |
Spring Boot注解
注解 | 场景说明 | 起始版本 |
@SpringBootConfiguration | Spring Boot配置类 | 1.4.0 |
@SpringBootApplication | Spring Boot应用引导注解 | 1.2.0 |
@EnableAutoConfiguration | Spring Boot激活自动转配 | 1.0.0 |