spring boot 配置文件application

场景:在项目部署的过程当中,对于spring boot的配置文件一直不很了解,直到项目出现一个莫名其妙的问题——工程classes中的配置文件被覆盖,程序启动老是报错!html

 

1  配置文件的优先级

application.properties你们都不陌生,咱们在开发的时候,常常使用它来配置一些能够手动修改并且不用编译的变量,这样的做用在于,打成war包或者jar用于生产环境时,咱们能够手动修改环境变量而不用再从新编译。java

spring boo默认已经配置了不少环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,能够在这里看spring boot默认的配置信息http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-configspring

1.1 配置项读取顺序

  1. 命令行参数
  2. 经过 SPRING_APPLICATION_JSON 设置的值
  3. JNDI attributes from java:comp/env
  4. Java 系统属性(System.getProperties())
  5. 操做系统环境变量
  6. A RandomValuePropertySource that only has properties in random.*.
  7. 项目 Jar 外的Profile 相关的配置文件(application-{profile}.properties)
  8. Jar 内的Profile 相关的配置文件(application-{profile}.properties)
  9. 项目 Jar 外的配置文件(application.properties)
  10. Jar 内的配置文件(application.properties)
  11. @PropertySource annotations on your @Configuration classes.
  12. 默认属性 (SpringApplication.setDefaultProperties)

1.2  application.properties 查找位置

除了在application.properties配置参数,还有多种方式能够设定参数。Spring Boot 按照下面的顺序查找配置项:json

spring boot容许你自定义一个application.properties文件,而后放在如下的地方,来重写spring boot的环境变量或者定义你本身环境变量tomcat

Spring Boot 默认从4个位置查找application.properties文件。markdown

  1. 当前目录下的/config目录
  2. 当前目录
  3. 类路径下的/config目录
  4. 类路径根目录

说明:
当前目录指的是运行Jar文件时的目录,不必定是jar文件所在目录,全部上面前2项是Jar包外目录。app

若是同时在四个地方都有配置文件,配置文件的优先级是从1到4。less

  • 当前目录,目前不知道如何肯定,可是spring在启动的时候会显示当前目录:

根据xxxApplicationStarter能够定位到当前目录。而后根据须要配置配置文件。dom

 

  • 类路径:

也就是classes目录。spring-boot

 1.3 读取和设置配置文件属性

参考文章http://www.nathanyan.com/2016/01/25/Spring-Boot-04-%E9%85%8D%E7%BD%AE%E7%9B%B8%E5%85%B3/

 使用配置文件以后,spring boo启动时,会自动把配置信息读取到spring容器中,并覆盖spring boot的默认配置,那么,咱们怎么来读取和设置这些配置信息呢

1.经过命令行来重写和配置环境变量,优先级最高,例如能够经过下面的命令来重写spring boot 内嵌tomcat的服务端口,注意“=”俩边不要有空格

java -jar demo.jar --server.port=9000

若是想要设置多个变量怎么办,能够已json的格式字符串来设置

java -jar demo.jar --spring.application.json='{"foo":"bar"}'

 

 

2.经过@value注解来读取

@RestController
@RequestMapping("/task")
public class TaskController {

@Value("${connection.remoteAddress}") private String address;

@RequestMapping(value = {"/",""})
public String hellTask(@Value("${connection.username}")String name){

    return "hello task !!";
}

}

 

 

3.经过Environment接口来获取,只须要把接口注进去便可

@RestController
@RequestMapping("/task")
public class TaskController {

@Autowired Environment ev ;

@Value("${connection.remoteAddress}") private String address;

@RequestMapping(value = {"/",""})
public String hellTask(@Value("${connection.username}")String name){

    String password = ev.getProperty("connection.password");
    return "hello task !!";
}

}

 

 

4.能够自定义一个工具类,来获取,这种方式关键在于读取配置文件信息,适合自定义的配置信息,spring 容器默认的配置信息会读不到

@Component
public class SystemConfig {

    private static Properties props ;

    public SystemConfig(){

        try {
            Resource resource = new ClassPathResource("/application.properties");//
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取属性
     * @param key
     * @return
     */
    public static String getProperty(String key){

        return props == null ? null :  props.getProperty(key);

    }

    /**
     * 获取属性
     * @param key 属性key
     * @param defaultValue 属性value
     * @return
     */
    public static String getProperty(String key,String defaultValue){

         return props == null ? null : props.getProperty(key, defaultValue);

    }

    /**
     * 获取properyies属性
     * @return
     */
    public static Properties getProperties(){
        return props;
    }

}

//用的话,就直接这样子
String value = SystemConfig.getProperty("key");

 

 

5.能够利用${…}在application.properties引用变量

myapp.name=spring
myapp.desc=${myapp.name} nice

 

6.能够在application.properties配置随机变量,利用的是RandomValuePropertySource类

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]}

 

 

7.绑定属性值到Bean

YAML文件

properties文件在面对有层次关系的数据时,就有点不合适。YAML 支持一种相似JSON的格式,能够表现具备层次的数据。详细说明看这里.
YAML的内容会转换为properties格式,以下:

environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App
my:
   servers:
       - dev.bar.com
       - foo.bar.com

 

 

environments.dev.url=http://dev.bar.com
environments.dev.name=Developer Setup
environments.prod.url=http://foo.bar.com
environments.prod.name=My Cool App
my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

能够用@Value("${environments.dev.url}")注入.

 

YAML的一个特性就是能够把多个文件的配置项,合并到一个文件里。用---分隔。

server:
    address: 192.168.1.100
---
spring:
    profiles: development
server:
    address: 127.0.0.1
---
spring:
    profiles: production
server:
    address: 192.168.1.120

再结合spring.profiles能够指定Profile下使用哪一个配置项.

Spring Boot 也支持Profile特性,Profile相关的配置文件命名为:application-{profile}.properties,能够用spring.profiles.active激活Profile:

java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=test

 

 

虽然能够经过@Value("${property}")注入属性值,若是有多项须要注入,就有点麻烦了。@ConfigurationProperties能够直接把多个属性值绑定到Bean上。

配置文件:

# application.yml

connection:
    username: admin
    remoteAddress: 192.168.1.1

 

使用:

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {
    private String username;
    private InetAddress remoteAddress;
    // ... getters and setters
}

 

 

也能够在建立Bean时注入:

@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
    ...
}

 

 

 

3