摘要: Spring Boot 核心特性之组件自动装配
正文:
Spring Boot 核心特性之组件自动装配
Spring Framework 手动装配
模式注解装配
- ex:
@Component
、@Service
、@Configuration
… - 装配方式:
<context:componet-scan>
或@ComponentScan
@Component
是一种由Spring 容器托管的通用模式组件
Spring Framework 注解 | 场景 | version |
---|---|---|
@Repository |
数据仓储模式注解 | 2.0 |
@Component |
通用组件模式注解 | 2.5 |
@Service |
服务模式注解 | 2.5 |
@Controller |
Web 控制器模式注解 | 2.5 |
@Configuration |
配置类模式注解 | 3.0 |
- 自定义模式注解
“派生性”(“基注解”<”派生注解”),可以参考以上注解实现自定义注解:
@Component
<@Repository
<CustomRepository
@Enable 模块装配
Spring Framework从3.1开始支持@Enable 模块驱动
,模块是指具备相同领域的功能组件集合组合成为一个独立的单元
举例:
@Enable 注解模块 | 模块说明 |
---|---|
@EnableWebMvc |
Web MVC模块 |
@EnableAsync |
异步处理模块 |
@EnableAutoConfiguration |
自动配置模块 |
@EnableEurekaClient |
Eureka Client模块 |
注解驱动方式
version:3.0
举例:
org.springframework.web.servlet.config.annotation.EnableWebMvc
1
2
3
4
5
public EnableWebMvc {}org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
1
2
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {}接口编程方式
version:3.1
举例:
org.springframework.boot.autoconfigure.EnableAutoConfiguration
1
2
3
4
5
public EnableAutoConfiguration {}org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector
1
2
3
4
5
6
7
8
9public class EnableAutoConfigurationImportSelector
extends AutoConfigurationImportSelector {}
public class AutoConfigurationImportSelector
implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware, Ordered {
public String[] selectImports(AnnotationMetadata annotationMetadata) {}
}
条件装配
Spring Framework从3.1开始支持在Bean 装配时增加前置条件判断
@Profile 配置化方式条件装配
version:3.1
举例:
- class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class StandaloneDataConfig {
public DataSource dataSource() {}
}
public class JndiDataConfig {
public DataSource dataSource() throws Exception {}
}- method
1
2
3
4
5
6
7
8
9
10
11
public class SomeConfig {
public DataSource standaloneDataSource() {}
public DataSource jndiDataSource() throws Exception {}
}- 激活
1
2
3
4AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();@Conditional 编程方式条件装配
version:4.0
举例:
1
2
3
4
5
public ConditionalOnClass {}Spring-Boot的@Conditional:
- ConditionalOnBean
- ConditionalOnMissingBean
- ConditionalOnClass
- ConditionalOnMissingClass
- ConditionalOnProperty
- ConditionalOnJava
- ConditionalOnWebApplication
Spring Boot 自动装配
激活:
@EnableAutoConfiguration
Spring Boot 默认没有激活自动装配,存在
@SpringBootApplication
注解中参考:org.springframework.boot.autoconfigure.AutoConfigurationImport
Selector#getCandidateConfigurations
配置:
/META-INF/spring.factories
规约文件,
META-INFO
指的是元信息目录,如spring.handlers
、spring.schemas
等参考:
org.springframework.core.io.support.SpringFactoriesLoader
实现:
XxxAutoConfiguration
ex:
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Demo-Spring-Boot-Starter
源码分析
ImportSelector
部分
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#selectImports
1 |
|
调用ImportSelector
部分
1 | org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors |