三、Spring Boot 配置详解

配置随机值

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

访问命令行属性

默认情况下,SpringApplication 将命令行选项参数( 参数以 – 开头,例如 java -jar demo.jar –server.port=9000 )传递给一个 property ,然后将它们添加到 Spring 的Environment。就像前面所说的,命令行配置的属性总是比其它属性源优先级要高。

如果你不想将命令行的属性添加到Environment,你可以:

SpringApplication.setAddCommandLineProperties(false)

应用属性文件

SpringApplicationapplication.properties文件中加载属性,然后将它们添加到 Spring 的Environment中。SpringApplication会从以下目录寻找属性文件(按从上到下的顺序):
1. 当前目录的/config子目录
2. 当前目录
4. classpath 下的/config
5. classpath 根目录

注意:
你也可以使用yaml文件替代properties文件。

指定配置文件路径

指定配置文件名:

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

指定配置文件路径:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

application-{profile}.properties.

通过spring.profiles.active进行多环境配置。

application-dev.properties--》 开发环境
application-test.properties--》 测试环境
application-prod.propertis --》生产环境

// 加载生产环境配置
java -jar xxx.jar --spring.profiles.active=prod 

使用yaml

只要有你的classpath路径下有SnakeYAML类库时,SpringApplication自动支持Yaml 作为 properties的替代。

加载yaml

Spring 框架提供了两种方式加载 YAML 文件:

  • YamlPropertiesFactoryBean 将 yaml 作为 Properties 来加载
  • YamlMapFactoryBean 将 yaml 作为 Map 来加载

例如:

environments:
 dev:
 url: http://dev.example.com
 name: Developer Setup
 prod:
 url: http://another.example.com
 name: My Cool App

===>>

environments.dev.url=http://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=http://another.example.com
environments.prod.name=My Cool App

-------------------------------------------------------------
my:
servers:
 - dev.example.com
 - another.example.com

===>> 
my.servers[0]=dev.example.com
my.servers[1]=another.example.com

使用@ConfigurationProperties绑定属性到类。

@ConfigurationProperties(prefix="my")
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
 return this.servers;
 }
}
单个yaml文件配置多环境
server:
 address: 192.168.1.100
---
spring:
 profiles: dev
server:
 address: 127.0.0.1
---
spring:
 profiles: prod
server:
 address: 192.168.1.120

// 如果spring.profiles.active=dev,server.address=127.0.0.1
// 如果spring.profiles.active=prod,server.address=192.168.1.120
yaml 缺陷

YAML文件不能使用@PropertySource来加载。

@PropertySource 引入配置文件

在多数情况下,配置信息基本上都是放入application.properties文件中,但在一些场景下,比如某个配置项比较多时,为了分开存放,也可自定义配置文件,如my.properties。由于自定义的文件,系统不会自动加载,这个时候就需要手动引入了。

利用@PropertySource注解既可以引入配置文件,需要引入多个时,可使用@PropertySources设置数组,引入多个文件。

@SpringBootApplication
@PropertySource(value="classpath:my.properties",encoding="utf-8")
public class Chapter3Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter3Application.class, args);
    }
}

@ConfigurationProperties 和 @EnableConfigurationProerties

config.code=code
config.name=小苗
config.hobby[0]=看电影
config.hobby[1]=旅游

实体类:
//@Component
@EnableConfigurationProperties(value= {Config.class})
@ConfigurationProperties(prefix="config")
@Data
public class Config {

    String code;

    String name;

    List<String> hobby;
}

利用@ConfigurationProperties属性,即可完成配置文件绑定到类。

使用@EnableConfigurationProperties注解将此配置实体注册为bean,或者直接加入@Component使其在启动时被自动扫描到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值