spring boot 配置文件application

阅读目录

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

 

回到顶部

1  配置文件的优先级

application.properties 大家都不陌生,我们在开发的时候,经常使用它来配置一些可以手动修改而且不用编译的变量,这样的作用在于,打成 war 包或者 jar 用于生产环境时,我们可以手动修改环境变量而不用再重新编译。

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

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 按照下面的顺序查找配置项:

spring boot 允许你自定义一个 application.properties 文件,然后放在以下的地方,来重写 spring boot 的环境变量或者定义你自己环境变量

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

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

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

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

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

根据 xxxApplicationStarter 可以定位到当前目录。然后根据需要配置配置文件。

 

  • 类路径:

也就是 classes 目录。

 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){

</span><span style="color: rgba(0, 0, 255, 1)">return</span> "hello task !!"<span style="color: rgba(0, 0, 0, 1)">;

}

}

 

 

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 </span>= ev.getProperty("connection.password"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "hello task !!"<span style="color: rgba(0, 0, 0, 1)">;

}

}

 

 

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

@Component
public class SystemConfig {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Properties props ;

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> SystemConfig(){

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        Resource resource </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathResource("/application.properties");<span style="color: rgba(0, 128, 0, 1)">//

props = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException e) {
e.printStackTrace();
}
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取属性
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> String getProperty(String key){

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> props == <span style="color: rgba(0, 0, 255, 1)">null</span> ? <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)"> :  props.getProperty(key);

}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取属性
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key 属性key
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> defaultValue 属性value
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> String getProperty(String key,String defaultValue){

     </span><span style="color: rgba(0, 0, 255, 1)">return</span> props == <span style="color: rgba(0, 0, 255, 1)">null</span> ? <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)"> : props.getProperty(key, defaultValue);

}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取properyies属性
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Properties getProperties(){
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> 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



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,让更多的人能够享受到获取知识的快乐!因为本人初入职场,鉴于自身阅历有限,所以本博客内容大部分来源于网络中已有知识的汇总,欢迎各位转载,评论,大家一起学习进步!如有侵权,请及时和我联系,切实维护您的权益!