1. 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. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
      2. @TestPropertySource annotations on your tests.
      3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
      4. Command line arguments.
      5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
        1. 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.
      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. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
      13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
        1. The spring.profiles.active property follows the same ordering rules as other properties: The highest PropertySource wins. This means that you can specify active profiles in application.properties and then replace them by using the command line switch.

          Sometimes, it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles. The SpringApplication entry point also has a Java API for setting additional profiles (that is, on top of those activated by the spring.profiles.active property). See the setAdditionalProfiles() method in SpringApplication.

          For example, when an application with the following properties is run by using the switch, --spring.profiles.active=prod, the proddb and prodmq profiles are also activated:

        2. ---
          my.property: fromyamlfile
          ---
          spring.profiles: prod
          spring.profiles.include:
          - proddb
          - prodmq
      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).
        1.  

          SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

          1. A /config subdirectory of the current directory
          2. The current directory
          3. A classpath /config package
          4. The classpath root

          The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

        2. If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:

          $ java -jar myproject.jar --spring.config.name=myproject

          The following example shows how to specify two locations:

          $ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
      16. @PropertySource annotations on your @Configuration classes.
      17. Default properties (specified by setting SpringApplication.setDefaultProperties).
  2. 转:https://blog.csdn.net/isea533/article/details/50281151
  3. Spring Boot 支持多种外部配置方式,这些方式优先级如下:
    1. 命令行参数
    2. 来自java:comp/env的JNDI属性
    3. Java系统属性(System.getProperties())
    4. 操作系统环境变量
    5. RandomValuePropertySource配置的random.*属性值
    6. jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
    7. jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
    8. jar包外部的application.properties或application.yml(不带spring.profile)配置文件
    9. jar包内部的application.properties或application.yml(不带spring.profile)配置文件
    10. @Configuration注解类上的@PropertySource
    11. 通过SpringApplication.setDefaultProperties指定的默认属性  
  4. 命令行参数
    1. 通过java -jar app.jar --name="Spring" --server.port=9090方式来传递参数。

      参数用--xxx=xxx的形式传递。

      可以使用的参数可以是我们自己定义的,也可以是Spring Boot中默认的参数。

      很多人可能会关心如web端口如何配置这样的问题,这些都是Spring Boot中提供的参数,部分可用参数如下:

    2. 注意:命令行参数在app.jar的后面!
    3. # LOGGING
      logging.path=/var/logs
      logging.file=myapp.log
      logging.config= # location of config file (default classpath:logback.xml for logback)
      logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF) # EMBEDDED SERVER CONFIGURATION (ServerProperties)
      server.port=
      server.address= # bind to a specific NIC
      server.session-timeout= # session timeout in seconds
      server.context-parameters.*= # Servlet context init parameters, e.g. server.context-parameters.a=alpha
      server.context-path= # the context path, defaults to '/'
      server.servlet-path= # the servlet path, defaults to '/'
    4. 更多常见的应用属性请浏览这里
  5. Java系统属性
    1. 注意Java系统属性位置java -Dname="isea533" -jar app.jar,可以配置的属性都是一样的,优先级不同。

      例如java -Dname="isea533" -jar app.jar --name="Spring!"中name值为Spring!

  6. 操作系统环境变量
    1. 配置过JAVA_HOME的应该都了解这一个。

      这里需要注意的地方,有些OS可以不支持使用.这种名字,如server.port,这种情况可以使用SERVER_PORT来配置。

      具体名字如何匹配,看本文后面。

  7. RandomValuePropertySource
    1. 系统中用到随机数的地方,例如: my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}12345

      random.int*支持value参数和,max参数,当提供max参数的时候,value就是最小值。

  8. 应用配置文件(.properties或.yml)
    1. 应用配置文件(.properties或.yml)

      在配置文件中直接写: name=Isea533 server.port=808012

      .yml格式的配置文件如: name: Isea533 server:     port: 8080123

      当有前缀的情况下,使用.yml格式的配置文件更简单。关于.yml配置文件用法请看这里 注意:使用.yml时,属性名的值和冒号中间必须有空格,如name: Isea533正确,name:Isea533就是错的。

      属性配置文件的位置 spring会从classpath下的/config目录或者classpath的根目录查找application.properties或application.yml。

      /config优先于classpath根目录

  9. @PropertySource
    1. 这个注解可以指定具体的属性配置文件,优先级比较低。
  10. SpringApplication.setDefaultProperties
    1. SpringApplication application = new SpringApplication(Application.class);
      Map<String, Object> defaultMap = new HashMap<String, Object>();
      defaultMap.put("name", "Isea-Blog");
      //还可以是Properties对象
      application.setDefaultProperties(defaultMap);
      application.run(args); @SpringBootApplication
      public class App
      {
      public static void main( String[] args )
      {
      SpringApplication.run(App.class, args);
      }
      }
  11. 应用(使用)属性
    1. @Value(“${xxx}”)
      1. 这种方式是最简单的,通过@Value注解可以将属性值注入进来。
    2. @ConfigurationProperties
      1. Spring Boot 可以方便的将属性注入到一个配置对象中。例如:
      2. name=isea533
        jdbc.username=root
        jdbc.password=root
        ... 对应的配置类: @ConfigurationProperties
        public class Config {
        private String name;
        private Jdbc jdbc;
        class Jdbc {
        private String username;
        private String password;
        //getter...
        } public Integer gePort(){
        return this.port;
        }
        public Jdbc getJdbc() {
        return this.jdbc;
        }
        } jdbc开头的属性都会注入到Jdbc对象中。
      3. 在@Bean方法上使用@ConfigurationProperties
        1. @ConfigurationProperties(prefix = "foo")
          @Bean
          public FooComponent fooComponent() {
          ...
          }
    3. 属性占位符
      1. app.name=MyApp
        app.description=${app.name} is a Spring Boot application
      2. 可以在配置文件中引用前面配置过的属性(优先级前面配置过的这里都能用)。

        通过如${app.name:默认名称}方法还可以设置默认值,当找不到引用的属性时,会使用默认的属性。

        由于${}方式会被Maven处理。如果你pom继承的spring-boot-starter-parent,Spring Boot 已经将maven-resources-plugins默认的${}方式改为了@ @方式,例如@name@。

        如果你是引入的Spring Boot,你可以修改使用其他的分隔符

    4. 属性名匹配规则
      1. 例如有如下配置对象:
        @Component
        @ConfigurationProperties(prefix="person")
        public class ConnectionSettings {
        private String firstName;
        } firstName可以使用的属性名如下:
        person.firstName,标准的驼峰式命名
        person.first-name,虚线(-)分割方式,推荐在.properties和.yml配置文件中使用
        PERSON_FIRST_NAME,大写下划线形式,建议在系统环境变量中使用
    5. 属性验证
      1. 可以使用JSR-303注解进行验证,例如:
        @Component
        @ConfigurationProperties(prefix="connection")
        public class ConnectionSettings { @NotNull
        private InetAddress remoteAddress; // ... getters and setters }

Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties的更多相关文章

  1. Spring boot 官网学习笔记 - Spring DevTools 介绍

    想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...

  2. Spring boot 官网学习笔记 - Spring Boot CLI 入门案例

    安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...

  3. Spring boot 官网学习笔记 - logging

    commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...

  4. Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)

    Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...

  5. Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利

    If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...

  6. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  7. Spring boot 官网学习笔记 - Configuration Class(@import)

    推荐使用 Java-based configuration ,也可以使用xml we generally recommend that your primary source be a single ...

  8. React官网学习笔记

    欢迎指导与讨论 : ) 前言 本文主要是笔者在React英文官网学习时整理的笔记.由于笔者水平有限,如有错误恳请指出 O(∩_∩)O 一 .Tutoial 篇 1 . React的组件类名的首字母必须 ...

  9. express官网学习笔记

    npm init 创建一个package.json npm install express --save-dev 安装到项目依赖 便于多人开发 路由结构定义 app.METHOD(PATH, HAND ...

随机推荐

  1. ZooKeeper异步调用命令

    在ZooKeeper中,所有的同步调用命令,都会有一个相应的异步调用方法.异步调用能在一个单独线程中同时提交更多的命令,也能在一定程度上简化代码实现. 1 异步create方法 如创建zNode的命令 ...

  2. ZOJ3435

    题意略. 思路: 将每一个点的坐标 (x,y,z) 与 (1,1,1) 相减,得到向量 (x - 1,y - 1,z - 1) 我们实际上就是要求出 这样互质的三元组有多少对就行了. 我们把这个长方体 ...

  3. Java多线程之线程的生命周期

    Java多线程之线程的生命周期 一.前言 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态.在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(R ...

  4. .gitignore不起作用,过滤规则

    git 通过配置.gitignore文件忽略掉的文件或目录,在.gitignore文件中的每一行保存一个匹配的规则 # 此为注释 – 将被 Git 忽略 *.a :忽略所有 .a 结尾的文件 !lib ...

  5. JavaScript 数据结构与算法之美 - 栈内存与堆内存 、浅拷贝与深拷贝

    前言 想写好前端,先练好内功. 栈内存与堆内存 .浅拷贝与深拷贝,可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScri ...

  6. JS之clientWidth、offsetWidth等属性介绍

    一.clientXXX 属性 代码演示 // css 部分 <style> .test{ width:100px; height:100px; border:1px solid red; ...

  7. Jconsole/jvisualvm远程监控weblogic中间件配置

    1.进入linu操作界面,进入到启动服务目录下 2.选择要监控的服务的启动项,进入到编辑状态(注意:要先将该文件进行备份),如下图所示 3.修改USER_AGRS域,添加如下内容,注意修改IP USE ...

  8. log4j配置相对路径

    整理自网上: 一般在我们开发项目过程中,log4j日志输出路径固定到某个文件夹,这样如果我换一个环境,日志路径又需要重新修改,比较不方便, 1.log4j的FileAppender本身就有这样的机制, ...

  9. 通知&代理

    通知:多对多的关系,比较耗性能 使用: 1.观察者到通知中心注册(接受那个发布者发布的什么通知,监听到通知后的处理方法)  [[NSNotificationCenter defaultCenter] ...

  10. POJ-2502 Subway( 最短路 )

    题目链接:http://poj.org/problem?id=2502 Description You have just moved from a quiet Waterloo neighbourh ...