首页  

springboot自动配置原理和实例     所属分类 springboot 浏览量 1025
自动配置注解 
@EnableAutoConfiguration
@Conditional
@EnableConfigurationProperties 
@ConfigurationProperties


@SpringBootApplication   @EnableAutoConfiguration

@EnableAutoConfiguration 通过 @Import 导入 EnableAutoConfigurationImportSelector

@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

AutoConfigurationImportSelector
public String[] selectImports(AnnotationMetadata annotationMetadata)

getCandidateConfigurations

SpringFactoriesLoader



List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
				getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());	
						
META-INF/spring.factories

public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader)

Enumeration<URL> urls = (classLoader != null ?
					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
					
FACTORIES_RESOURCE_LOCATION  META-INF/spring.factories
			



spring-boot-autoconfigure-2.0.0.RELEASE.jar

META-INF/spring.factories 配置信息 (部分)

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnClassCondition

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration



自动配置实例 MyAutoConfig MyBean src/main/resources/META-INF/spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration=autoconf.MyAutoConfig @Configuration public class MyAutoConfig{ @Bean public MyBean myBean(){ return new MyBean(); } } 完整代码 https://gitee.com/dyyx/springboothello/tree/master/src/main/java/autoconf
spring-boot-starter原理

上一篇     下一篇
延迟任务处理总结

springboot最核心的三个注解

springboot面试题

ConcurrentHashMap使用注意点

SpringCloud VS Dubbo

Jedis的几种使用方式