02-自动装配思维导图
2021-12-28 00:57:13 2 举报
AI智能生成
自动装配思维导图
作者其他创作
大纲/内容
@SpringBootApplication
@SpringBootConfiguration
@Configuration:
标识启动类是一个配置类
标识启动类是一个配置类
@Component:
标识将启动类注册为bean
标识将启动类注册为bean
@EnableAutoConfiguration
标识开启自动装配
@AutoConfigurationPackage
添加该注解的类所在的package作为自动配置package进行管理
内部静态类Registrar#registerBeanDefinitions方法
List<String>类型
@Import(AutoConfigurationImportSelector.class)
selectImports:
解决装配什么
解决装配什么
入参AnnotationMetadata:
注解元信息
注解元信息
AnnotationAttributes:
getAttributes(annotationMetadata)获取注解属性信息
getAttributes(annotationMetadata)获取注解属性信息
getCandidateConfigurations:
获取候选配置类
获取候选配置类
找EnableAutoConfiguration定义信息:
"META-INF/spring.factories"
"META-INF/spring.factories"
以MultiValueMap<String, String>形式存入缓存
配置类例子:
@Import({ JdbcTemplateConfiguration.class, NamedParameterJdbcTemplateConfiguration.class })
JdbcTemplateAutoConfiguration
@Import({ JdbcTemplateConfiguration.class, NamedParameterJdbcTemplateConfiguration.class })
JdbcTemplateAutoConfiguration
JdbcTemplateConfiguration:
@Bean
@Primary
JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
JdbcProperties.Template template = properties.getTemplate();
jdbcTemplate.setFetchSize(template.getFetchSize());
jdbcTemplate.setMaxRows(template.getMaxRows());
if (template.getQueryTimeout() != null) {
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
}
return jdbcTemplate;
}
@Bean
@Primary
JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
JdbcProperties.Template template = properties.getTemplate();
jdbcTemplate.setFetchSize(template.getFetchSize());
jdbcTemplate.setMaxRows(template.getMaxRows());
if (template.getQueryTimeout() != null) {
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
}
return jdbcTemplate;
}
removeDuplicates:
配置类去重
配置类去重
getExclusions:
获得待排除的类
获得待排除的类
exclude
例子(多个时逗号隔开):
exclude = {DataSourceAutoConfiguration.class}
exclude = {DataSourceAutoConfiguration.class}
excludeName
例子(需是类的全限定名,多个时逗号隔开):
excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
spring.autoconfigure.exclude
checkExcludedClasses:
校验待排除的类
校验待排除的类
判断要排除的类是否是自动装配配置类,不是则抛出异常
IllegalStateException:
"The following classes could not be excluded
because they are not auto-configuration classes:%n%s"
"The following classes could not be excluded
because they are not auto-configuration classes:%n%s"
配置类列表删除要排除的部分
配置类列表再次过滤
过滤器定义:
\META-INF\spring.factories
\META-INF\spring.factories
过滤器例子:
# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
作用:如果一个配置类依赖某个class,
但是这个class在classpath下找不到,
则这个配置类会被过滤,即不会作为自动装配对象
但是这个class在classpath下找不到,
则这个配置类会被过滤,即不会作为自动装配对象
启动日志(debug: true):
Negative matches
Negative matches
RedisCacheConfiguration:
Did not match:
- @ConditionalOnClass did not find required class
'org.springframework.data.redis.connection.RedisConnectionFactory' (OnClassCondition)
Did not match:
- @ConditionalOnClass did not find required class
'org.springframework.data.redis.connection.RedisConnectionFactory' (OnClassCondition)
fireAutoConfigurationImportEvents
new AutoConfigurationImportEvent
AutoConfigurationImportListener
listener.onAutoConfigurationImportEvent(event):
将配置类数组保存到监听器的一个对象
将配置类数组保存到监听器的一个对象
\META-INF\spring.factories
默认配置类定义文件例子:
\spring-boot-autoconfigure-2.3.12.RELEASE.jar!\META-INF\spring.factories
\spring-boot-autoconfigure-2.3.12.RELEASE.jar!\META-INF\spring.factories
@ComponentScan
标识开启组件扫描
扫描对象为启动类即引导类所在目录,
及其下子目录的bean定义对象
及其下子目录的bean定义对象
0 条评论
下一页
为你推荐
查看更多