前言
了解一件人最好的方式就是了解他的过去,那spring的过去是什么呢,他是怎么成长成现在的样子的呢? 现在就来聊一聊
Spirng1x 非注解驱动时代
jdk1.5时,java推出了注解开发,极大简化了开发流程,省了很多代码,spring也开始使用注解,但是在spring1x中是不支持注解的,只能通过xml配置
Spirng2x 注解过度时代
这时依赖注入注解@Autowire 依赖查找注解@Quafiler,组件声明注解@Component,@Service登场,还有@PostConstruct @PreDestory注解也替换了 xml的 init-mehod destory-method生命周期的回调接口,同时引入了@Scope 替换了 xml 的scope配置,但是注解的使用还是要通过xml的配置
<context:annotation-config>和<context:conponent-scan> 来实现注册,所以他并不完全
Spirng3x 注解黄金时代
spring3.0这时候出现了一个@Cofiguration,而且也引入了注解的派生性,注解时不能被继承的,但是却又派生特性,
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Cofiguration就是@Component的派生注解,而且这时就不用 <context:annotation-config>来激活注解了,但是仍然没有解决需要<context:conponent-scan> 来扫描的问题,但是做了一个过渡方案,就是使用@Import和@ImportRescource注解
@ImportResource("classpath:/META-INF/spring/others.xml")
@Configuration("springContextConfiguration")
public class SpringContextConfiguration {
但是这种也是有问题的,既然others.xml可以引导,那么写<context:conponent-scan>就是一件重复的事
spring3.1鉴于3.0版本的尴尬,3.1出现了一个替代<context:conponent-scan>的注解@ComponetScan,还有不同环境的激活注解@Profile
@Configuration("springContextConfiguration")
@ComponentScan(basePackages = "thinking.in.spring.boot.samples") // 替代<context:component-scan>
@Profile("!production") // 非生产环境
public class SpringContextConfiguration {
同时新增了@ResponseBody,将返回对象处理方法序列化为rest主体内容,@Enviroment,@PropertySources,实现spring外部化配置基础,提供异步化操作注解@Ansync,周期异步执行注解@Schecule,校验方面新增校验注解@Validate,缓存方面新增@Cache,还有@EnableMvc注解等等
Spirng4x 注解完善时代
@Profile提供了配置化条件组装,不过存在一个问题,他没办法根据条件来判断加载,所以4x中引入了条件化注解@Conditional,弥补可@Profile的短板,@Profile从4x开始也变成了由@Conditional来实现,并且SpringBoot中所有的@Conditional*注解都是由@Conditional派生的。
派生由于注解不存在继承,所以只能通过派生来处理,派生页有限制条件,他需要保持注解之间有相同的属性方法。为了解决这个限制@Aliasfor注解诞生了
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
这样@SpringBootApplication注解就有了@EnableAutoConfiguration注解的能力
Spirng5x 注解当下时代
5x是springboot2.0的底层框架,其中引入了@indexed注解提升扫描效率,以及@NonNull,@Nullable注解等