SpringBoot(八):springboot的自動配置原理

Spring Boot在進行SpringApplication對象實例化時會加載META-INF/spring.factories文件,將該配置文件中的配置載入到Spring容器。

1.maven下載源碼

    通過dependency:sources 該命令可以下載該項目中所有的依賴的包的源碼。

2.META-INF/spring.factories文件在哪裏

    spring-boot-1.5.2.RELEASE.jar/META-INF/spring.factories

3.源碼分析

org.springframework.boot.SpringApplication:


org.springframework.core.io.support.SpringFactoriesLoader:



由此可見,讀取該配置文件來加載內容。

4.舉例Redis的自動配置

    從上述的配置中可以看出,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration是Redis的自動配置



Redis在全局文件中的配置

5.條件註解



# REDIS (RedisProperties)
spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL, will override host, port and password (user will be ignored), e.g. redis://user:[email protected]:6379
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.ssl=false # Enable SSL support.
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
spring.redis.timeout=0 # Connection timeout in milliseconds.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章