本篇来学习spring boot项目如何来正确配置。
spring-boot的配置文件默认是application.properties
, 但我建议大家使用application.yml
,因为spring-boot的官方配置文档都用的是.yml
。其中这里是一份关于yaml
,也就是.yml
的教程。
配置的顺序和优先级
要了解spring-boot的配置,那就必须要了解spring 配置文件加载的顺序和优先级
由于上面文档是英文,特翻译一下,但由于更新等问题,还是建议查阅官方文档
先放上英文原文:
- Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
- @TestPropertySource
annotations on your tests. - @SpringBootTest#properties
annotation attribute on your tests. - Command line arguments.
- Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
- ServletConfig init parameters.
- ServletContext init parameters.
- JNDI attributes from java:comp/env.
- Java System properties (System.getProperties()).
- OS environment variables.
- A RandomValuePropertySource, that only has properties in random.*.
- Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
- Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
- Application properties outside of your packaged jar (application.properties and YAML variants).
- Application properties packaged inside your jar (application.properties and YAML variants).
- @PropertySource annotations on your @Configuration classes.
- Default properties (specified using SpringApplication.setDefaultProperties).
中文版:
- 开发工具的配置项
- 测试配置项
- 测试配置项
- 命令行参数, 例如
java -jar app.jar --name="Spring"
- 命令行中的
SPRING_APPLICATION_JSON
JSON字符串, 例如java -Dspring.application.json='{"foo":"bar"}' -jar myapp.jar
- ServletConfig 初始化参数,可在代码进行配置
- ServletContext 初始化参数,可在代码进行配置
- JNDI参数, 例如
java:comp/env/spring.application.json
- Java系统参数
- 操作系统参数
- Random生产参数,仅仅匹配random.*。例如
my.number=${random.int}
my.number.in.range=${random.int[1024,65536]}
- jar外部的带指定profile的application.yml,比如
application-{profile}.yml
- jar内部的带指定profile的application.yml,比如
application-{profile}.yml
- jar外部的application.yml
- jar内部的application.yml
- 在自定义的
@Configuration
类中定于的@PropertySource
- 启动的main方法中,定义的默认配置。
SpringApplication.setDefaultProperties
有哪些配置可以使用
要搞清楚在application.yml
中有哪些配置可用,就必须要了解spring配置文件是如何去解析的。
通过研究代码发现如下顺序和加载列表
main:12, ShallyCoreApplication (com.piggsoft)
run:1175, SpringApplication (org.springframework.boot)
run:1186, SpringApplication (org.springframework.boot)
run:315, SpringApplication (org.springframework.boot)
refreshContext:371, SpringApplication (org.springframework.boot)
refresh:761, SpringApplication (org.springframework.boot)
refresh:122, EmbeddedWebApplicationContext (org.springframework.boot.context.embedded)
refresh:524, AbstractApplicationContext (org.springframework.context.support)
invokeBeanFactoryPostProcessors:686, AbstractApplicationContext (org.springframework.context.support)
invokeBeanFactoryPostProcessors:93, PostProcessorRegistrationDelegate (org.springframework.context.support)
invokeBeanDefinitionRegistryPostProcessors:270, PostProcessorRegistrationDelegate (org.springframework.context.support)
postProcessBeanDefinitionRegistry:246, ConfigurationClassPostProcessor (org.springframework.context.annotation)
processConfigBeanDefinitions:324, ConfigurationClassPostProcessor (org.springframework.context.annotation)
parse:184, ConfigurationClassParser (org.springframework.context.annotation) 其中166行为导入用户配置
processDeferredImportSelectors:475, ConfigurationClassParser (org.springframework.context.annotation)
查询classpath下,所有的META-INF/spring.factories
,然后得出
0 = "org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration"
1 = "org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration"
2 = "org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration"
3 = "org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration"
4 = "org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration"
5 = "org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration"
6 = "org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration"
7 = "org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration"
8 = "org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration"
9 = "org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration"
10 = "org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration"
11 = "org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration"
12 = "org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration"
13 = "org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration"
14 = "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
15 = "org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration"
16 = "org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration"
17 = "org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration"
18 = "org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration"
19 = "org.springframework.boot.autoconfigure.aop.AopAutoConfiguration"
20 = "org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration"
21 = "org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration"
22 = "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration"
23 = "org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration"
24 = "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration"
25 = "org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration"
26 = "org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration"
27 = "org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration"
28 = "org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration"
29 = "org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration"
30 = "org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration"
31 = "org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration"
32 = "org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration"
33 = "org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration"
34 = "org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration"
35 = "org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration"
36 = "org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration"
37 = "org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration"
38 = "org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration"
39 = "org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration"
40 = "org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration"
41 = "org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration"
42 = "org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration"
43 = "org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration"
44 = "org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration"
45 = "org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration"
46 = "org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration"
47 = "org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration"
48 = "org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration"
49 = "org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration"
50 = "org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration"
51 = "org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration"
52 = "org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration"
53 = "org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration"
54 = "org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration"
55 = "org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration"
56 = "org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration"
57 = "org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration"
58 = "org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration"
59 = "org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration"
60 = "org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration"
61 = "org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration"
62 = "org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration"
63 = "org.springframework.boot.autoconfigure.jms.hornetq.HornetQAutoConfiguration"
64 = "org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration"
65 = "org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration"
66 = "org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration"
67 = "org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration"
68 = "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"
69 = "org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration"
70 = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration"
71 = "org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration"
72 = "org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration"
73 = "org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration"
74 = "org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration"
75 = "org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration"
76 = "org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration"
77 = "org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration"
78 = "org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration"
79 = "org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration"
80 = "org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration"
81 = "org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration"
82 = "org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration"
83 = "org.springframework.boot.autoconfigure.session.SessionAutoConfiguration"
84 = "org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration"
85 = "org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration"
86 = "org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration"
87 = "org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration"
88 = "org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration"
89 = "org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration"
90 = "org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration"
91 = "org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration"
92 = "org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration"
上面的大部分都是在spring-boot-autoconfigure-xx.jar
中.
比如要配置端口之类,那么久去查看org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
,通过文档发现其配置指向了org.springframework.boot.autoconfigure.web.ServerProperties
,我们就可以通过观察代码来对相关属性进行配置。