超简单的springboot自动配置原理分析

springboot自动配置确实帮我们省去了配置上的麻烦

先看一下手动配置的繁琐: 

/**
 * @Author: guandezhi
 * @Date: 2019/3/19 20:53
 */
@Configuration
public class DataSourceConfig {


    @Value("${spring.datasource.user.driveClassName}")
    private String driveClassName;
    @Value("${spring.datasource.user.url}")
    private String url;
    @Value("${spring.datasource.user.username}")
    private String username;
    @Value("${spring.datasource.user.password}")
    private String passworld;

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driveClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(passworld);
        return dataSource;
    }

}

 以上是注解方式配置数据源,如果配置文件比较多的话,手动配置还是比较麻烦,不管是xml还是注解方式。

进入到springboot自动配置包下的META-INF/spring.factories目录下,可以看到key=org.springframework.boot.autoconfigure.EnableAutoConfiguration的value为一系列类的全限定名

# 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,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\

 以上只截取了META-INF/spring.factories目录下部分自动配置的key和一系列全限定名

其实pringboot已经帮我们写好了java注解版的配置,并在符合条件的情况下注入到spring容器。

@ConditionalOnClass({DataSource.class, EmbeddedDatabaseType.class}) 这里只有在类路径下有DataSource这个类,并且是EmbeddedDatabaseType这种数据库类型该数据源自动配置才会生效。

每一个自动配置都有一个对应的XXX.Properties,这里数据源对应的是:DataSourceProperties

这样,只有我们按照spring.datasource开头的数据源配置文件,就可以和DataSourceProperties绑定并赋值。

为什么能够实现自动配置,就是因为springboot,已经为我们写好了一系列的java注解配置,只是在等待一个条件触发其生效而已。

启动类上的@SpringBootApplication ==》@EnableAutoConfiguration ==》@Import({AutoConfigurationImportSelector.class})

最后查找META-INF/spring.factories文件中包含的JAR文件。

总结:

    1.启动类上的@SpringBootApplication已经帮我们开启了自动配置。

    2.springboot已经帮我们写好了一系列按java注解方式的配置文件,只是在等待某个条件触发生效而已。

    3. 我们的配置文件只能按springboot提供的前缀来写,才能自动配置,这也失去了一些配置文件的灵活性。

扫描二维码关注公众号,回复: 5896245 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_35859844/article/details/88673962