超簡單的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提供的前綴來寫,才能自動配置,這也失去了一些配置文件的靈活性。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章