首先要说一下版本相关,Spring Framework3.1开始引入了@Enable注解。
何为模块
是指具有相同领域的功能组件集合,组合锁形成的一个独立的单元。
比如:
做过微服务的小伙伴都应该见过
@EnableEurekaServer
@EnableFeignClients
@EnableWebMvc
等以@Enable开头的注解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {
//.....省略
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
//....省略
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
细心的人会看到上面几个注解上都有@Import注解,具体原因后面再说。
框架设计者有意识低形成一种设计模式,但是和传统的面向对象的Gof 23种设计模式不同,以下便是以@Enable作为前缀的相关注解:
Spring framework中
Spring Boot中
Spring Cloud 中
有何意义?
简化装配步骤,实现了需要的时候再进行装配,同事也规避了组件集合配置的细节问题。