springboot自动装配原理

编程

springboot自动装配原理

SpringBootApplication注解

@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

 

@EnableAutoConfiguration完成了自动装配

@EnableXxx 注解一般用于开启某一项功能,是为了简化代码的导入,即使用了该类注

解,就会自动导入某些类。所以该类注解是组合注解,一般都会组合一个

@Import 注解,用

于导入指定的多个类,而被导入的类一般分为三种:配置类、选择器,与注册器。

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@AutoConfigurationPackage

@Import(AutoConfigurationImportSelector.class)

public @interface EnableAutoConfiguration

AutoConfigurationImportSelector类实现了查找XXAutoConfiguration类。核心方法

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {

    // 查找jar包下面的META-INF/spring.factories

List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),

getBeanClassLoader());

Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "

+ "are using a custom packaging, make sure that file is correct.");

return configurations;

}

 spring.factories文件

# AutoConfigureCache auto-configuration imports

org.springframework.boot.test.autoconfigure.core.AutoConfigureCache=

org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration

# AutoConfigureDataJdbc auto-configuration imports

org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc=

org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,

org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,

org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,

org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureDataJpa auto-configuration imports

org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa=

org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,

org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,

org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,

org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,

org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureDataLdap auto-configuration imports

org.springframework.boot.test.autoconfigure.data.ldap.AutoConfigureDataLdap=

org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,

org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,

org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration

# AutoConfigureDataMongo auto-configuration imports

org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo=

org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,

org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,

org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,

org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,

org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,

org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,

org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration

# AutoConfigureDataNeo4j auto-configuration imports

org.springframework.boot.test.autoconfigure.data.neo4j.AutoConfigureDataNeo4j=

org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,

org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,

org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureDataRedis auto-configuration imports

org.springframework.boot.test.autoconfigure.data.redis.AutoConfigureDataRedis=

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,

org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

# AutoConfigureJdbc auto-configuration imports

org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=

org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,

org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,

org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureTestDatabase auto-configuration imports

org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase=

org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

# AutoConfigureJooq auto-configuration imports

org.springframework.boot.test.autoconfigure.jooq.AutoConfigureJooq=

org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,

org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,

org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,

org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,

org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

# AutoConfigureJson auto-configuration imports

org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=

org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,

org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,

org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration

# AutoConfigureJsonTesters auto-configuration imports

org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters=

org.springframework.boot.test.autoconfigure.json.JsonTestersAutoConfiguration,

org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,

org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,

org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration

# AutoConfigureWebClient auto-configuration imports

org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient=

org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,

org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,

org.springframework.boot.test.autoconfigure.web.reactive.WebTestClientAutoConfiguration

# AutoConfigureWebFlux auto-configuration imports

org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux=

org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,

org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,

org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,

org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,

org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,

org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,

org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,

org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration

# AutoConfigureMockMvc auto-configuration imports

org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=

org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration,

org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration,

org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration,

org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration

# AutoConfigureMockRestServiceServer

org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer=

org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerAutoConfiguration

# AutoConfigureRestDocs auto-configuration imports

org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs=

org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration

# AutoConfigureTestEntityManager auto-configuration imports

org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager=

org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration

# AutoConfigureWebClient auto-configuration imports

org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient=

org.springframework.boot.test.autoconfigure.web.client.WebClientRestTemplateAutoConfiguration,

org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,

org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,

org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,

org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,

org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,

org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,

org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration

# AutoConfigureWebMvc auto-configuration imports

org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc=

org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,

org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,

org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,

org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,

org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,

org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,

org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,

org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,

org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,

org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,

org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,

org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,

org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,

org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

# DefaultTestExecutionListenersPostProcessors

org.springframework.boot.test.context.DefaultTestExecutionListenersPostProcessor=

org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener$PostProcessor

# Spring Test ContextCustomizerFactories

org.springframework.test.context.ContextCustomizerFactory=

org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory,

org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory,

org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizerFactory,

org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory

# Test Execution Listeners

org.springframework.test.context.TestExecutionListener=

org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,

org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,

org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,

org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener

 

redis的自动装配过程

@Configuration

// 只有找到了RedisOperations这个类才会加载配置

@ConditionalOnClass(RedisOperations.class)

// 需要Redis的相关配置

@EnableConfigurationProperties(RedisProperties.class)

//导入jedis

@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })

publicclass RedisAutoConfiguration {

@Bean

  //没有RedisTemplate的时候就会产生一个

@ConditionalOnMissingBean(name = "redisTemplate")

public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)

throws UnknownHostException {

RedisTemplate<Object, Object> template = new RedisTemplate<>();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

@Bean

@ConditionalOnMissingBean

public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)

throws UnknownHostException {

StringRedisTemplate template = new StringRedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

}

 

加载redis的时候,判断jvm里面有没redis的相关类,配置文件中有相关配置,这样自动配置才会生效,灵活配置。

 

以上是 springboot自动装配原理 的全部内容, 来源链接: utcz.com/z/514956.html

回到顶部