博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot加载顺序
阅读量:6871 次
发布时间:2019-06-26

本文共 3356 字,大约阅读时间需要 11 分钟。

  hot3.png

当团队壮大后,往往不需要让开发人员关心各个环境细节,一般由模块负责人或者运维集中维护,我们团队由运维提供组件部署信息,模块负责人维护各自环境配置

为合理灵活的支持配置管理,springboot提供覆盖性,优先级化的加载顺序

目前工程只用了command line、内外部yaml文件配置、部分工程用了OS environment variables.

Externalized Configuration

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans by using the @Valueannotation, accessed through Spring’s Environment abstraction, or be through @ConfigurationProperties.

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

  1. on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. annotations on your tests.
  3. properties attribute on your tests. Available on and the .
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that has properties only in random.*.
  12. outside of your packaged jar (application-{profile}.properties and YAML variants).
  13. packaged inside your jar (application-{profile}.properties and YAML variants).
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. annotations on your @Configuration classes.
  17. Default properties (specified by setting SpringApplication.setDefaultProperties).

To provide a concrete example, suppose you develop a @Component that uses a name property, as shown in the following example:

import org.springframework.stereotype.*;import org.springframework.beans.factory.annotation.*;@Componentpublic class MyBean {    @Value("${name}")    private String name;    // ...}

On your application classpath (for example, inside your jar) you can have an application.properties file that provides a sensible default property value for name. When running in a new environment, an application.properties file can be provided outside of your jar that overrides the name. For one-off testing, you can launch with a specific command line switch (for example, java -jar app.jar --name="Spring").

The SPRING_APPLICATION_JSON properties can be supplied on the command line with an environment variable. For example, you could use the following line in a UN*X shell:

$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar

In the preceding example, you end up with acme.name=test in the Spring Environment. You can also supply the JSON as spring.application.json in a System property, as shown in the following example:

$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar

You can also supply the JSON by using a command line argument, as shown in the following example:

$ java -jar myapp.jar --spring.application.json='{"name":"test"}'

You can also supply the JSON as a JNDI variable, as follows: java:comp/env/spring.application.json

参考文档:

转载于:https://my.oschina.net/yugj/blog/2998483

你可能感兴趣的文章
JPA mappedBy属性
查看>>
开启服务器Mcrypt.so加密库的方法
查看>>
如何将SWT程序移植到Applet
查看>>
去好店,一个人在城市里面发生的故事
查看>>
点在面内(2)
查看>>
SPRING注解发布RMI/HTTPInvoker/Hessian/Burlap服务
查看>>
Jmeter(一)-精简测试脚本
查看>>
PowerDesigner显示Comment注释
查看>>
Learn Python the Hard Way: 类(Class)和对象(Object)
查看>>
centos安装或修复grub并使用grub引导系统
查看>>
ThinkPHP 上传文件方法
查看>>
Linux 系统 网卡RTL8723BE 信号差不稳定的解决办法
查看>>
getopt, optarg, optind, opterr, optopt
查看>>
为什么你这么努力,工作却还是没有起色?
查看>>
今天看了thinkcmf 和onethink
查看>>
root运行google chrome
查看>>
SSH部署到Tomcat内存溢出-OutOfMemoryError 的解决办法
查看>>
Android Studio SDK报错,所有方法均红色警告,但可正常编译运行
查看>>
VC使用ADO连接SQLServer数据库
查看>>
JS获取文字长度
查看>>