码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot 属性配置

时间:2017-11-19 22:17:38      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:加载顺序   util   系统环境   enc   框架   安装   conf   目录   test   

属性的配置在项目中经常被使用到,Spring Boot 相对spring框架配置文件对属性进行配置简化了很多。

一:属性配置优先级

 

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指定的默认属性

(1.1)命令行参数

通过java -jar myApp.jar --name="Spring Boot" --server.port=8081方式来传递参数。命令行参数在myApp.jar 后面设置。

(1.2)J2EE 上下文环境变量

(1.3)java系统变量

java -Dname="ws" -jar myApp.jar,设置name属性为ws

如果设置java -Dname="ws" -jar app.jar --name="Boot"   由于命令行参数优先级高,则name属性为Boot

(1.4)操作系统环境变量

操作系统环境变量如安装JAVA和MAVEN时配置的JAVA_HOME,MAVEN_HOME等,这些都是操作系统的环境变量。

(1.5)随机值

# 随机字符串
com.ws.blog.value=${random.value}
# 随机int
com.ws.blog.number=${random.int}
# 随机long
com.ws.blog.bignumber=${random.long}
# 10以内的随机数
com.ws.blog.test1=${random.int(10)}
# 10-20的随机数
com.ws.blog.test2=${random.int[10,20]}

(1.6)@PropertySource

这个注解可以指定具体的属性配置文

 

二:application.properties文件加载顺序

1. 当前路径下的/config子目录。
2. 当前路径。
3. classpath路径下的/config子路径。
4. classpath路径

 

三:配置属性类

(3.1) 采用@Value("${XXX}")配置

 1 @Configuration
 2 public class MyConfig {
 3     @Value("${ws.name}")
 4     private String name;
 5     @Value("${ws.age}")
 6     private Integer age;
 7 
 8     public String getName() {
 9         return name;
10     }
11 
12     public void setName(String name) {
13         this.name = name;
14     }
15 
16     public Integer getAge() {
17         return age;
18     }
19 
20     public void setAge(Integer age) {
21         this.age = age;
22     }
23 }

(3.2)采用@ConfigurationProperties 配置

首先在pom中添加如下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

application.properties文件中添加如下配置

#My Config
ws.name=ws@ConfigurationProperties
ws.age=18
ws.listAddress[0]=add1
ws.listAddress[1]=add2

配置MyConfig类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "ws")
public class MyConfig {

    private String name;

    private Integer age;

    private List<String> listAddress;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<String> getListAddress() {
        return listAddress;
    }

    public void setListAddress(List<String> listAddress) {
        this.listAddress = listAddress;
    }
}

 

Spring Boot 属性配置

标签:加载顺序   util   系统环境   enc   框架   安装   conf   目录   test   

原文地址:http://www.cnblogs.com/fdzfd/p/7862016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!