<aside> <img src="/icons/list-indent_red.svg" alt="/icons/list-indent_red.svg" width="40px" /> TOC
</aside>
<aside> 👉🏼 Q: spring boot 配置文件生效以及优先级关系
如果bootstrap.yml配了 active: local 而且application.yml 配了 active: test 那么配置文件加载顺序是如何? 如果所有配置文件都配置了 datasource: url: 那么会以哪个配置文件为准?
A:
在Spring Boot中,bootstrap.yml
和application.yml
的加载顺序是:先加载bootstrap.yml
,然后加载application.yml
。在这两个文件中,spring.profiles.active
属性的设置会决定哪些特定的配置文件会被加载。
在你的情况中,bootstrap.yml
中设置了spring.profiles.active: local
,这意味着bootstrap-local.yml
会被加载。然后,application.yml
中设置了spring.profiles.active: test
,这会覆盖之前的设置,导致application-test.yml
(如果存在)被加载。
关于datasource.url
的配置,如果在多个配置文件中都有设置,那么会按照以下的优先级顺序来决定最终的值:
application-test.yml
(如果存在)application.yml
bootstrap-local.yml
bootstrap.yml
这是因为application.yml
在bootstrap.yml
之后加载,所以application.yml
中的配置会���盖bootstrap.yml
中的同名配置。同样,application-test.yml
在application.yml
之后加载,所以application-test.yml
中的配置会覆盖application.yml
中的同名配置。
</aside>
在你的配置中,你已经排除了Spring Boot的默认数据源自动配置DataSourceAutoConfiguration
,这意味着你正在使用自定义的数据源配置。这是一个很好的优化,因为它允许你完全控制你的数据库连接和连接池的配置。
然而,还有一些其他的优化可以考虑:
@Lazy
注解来实现。maxActive
的值,这将减少系统资源的使用。logAbandoned
为false
来实现。filter
配置来关闭它。以下是优化后的配置:
spring:
profiles:
active: bootstrap-test # 只是测试
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
datasource:
url: jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&net_read_timeout=60
druid:
initial-size: 1
min-idle: 1
maxActive: 3
maxWait: 60000
phyMaxUseCount: 2000
logAbandoned: false
filter:
wall:
config:
multi-statement-allow: false
none-base-statement-allow: false
main:
lazy-initialization: true # 全局懒加载
请注意,这些只是一般的建议,具体的配���可能需要根据你的应用程序的具体需求和数据库的性能来调整��