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

SpringBoot 外部配置

时间:2021-01-08 10:51:04      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:new   not   地方   package   其他   项目   code   查找文件   features   

  本篇文章主要介绍SpringBoot 配置相关内容。如果文章中有错误或不明确的地方,请大家望指正,谢谢!

https://docs.spring.io/spring-boot/docs/2.3.7.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config

 

配置随机值

 RandomValuePropertySource 可以用于注入随机值,要求属性中配置必须以 random开头;

application.yaml

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

 

  

  •  ${random.int} 生成的有整数包含符号,正数、负数、0
  • ${random.int[1024,65536]} 生成范围内的整数,包括下边界值,不包含上边界 值计算方式:start + new Random().nextInt(65536-1024)
  • ${random.int(10)}  0-10之间的整数,不包括10
  • 其他情况 例如:${random.str}   生成一个随机长度为32的字节数组,MD5摘要算法(长度为128bit),再转化为16进制输出.  最终输出长度为32位字符串

Application.java

package com.wm8.logging;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

 

Runner.java
package com.wm8.logging;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class Runner implements ApplicationRunner {
    
    @Autowired
    Environment environment;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String property1 = environment.getProperty("my.bignumber");
        String property2 = environment.getProperty("my.number");
        String property3 = environment.getProperty("my.number.in.range");
        String property4 = environment.getProperty("my.number.less.than.ten");
        String property5 = environment.getProperty("my.secret");
        String property6 = environment.getProperty("my.uuid");
        System.out.println("my.bignumber->"+property1);
        System.out.println("my.number->"+property2);
        System.out.println("my.number.in.range->"+property3);
        System.out.println("my.number.less.than.ten->"+property4);
        System.out.println("my.secret->"+property5);
        System.out.println("my.uuid->"+property6);
    }

}

启动后输入内容如下

my.bignumber->-6342504894010926224
my.number->-1804922465
my.number.in.range->1024
my.number.less.than.ten->9
my.secret->2911b6d4147c57529ac276a4649d2c3b
my.uuid->a246af3e-05cc-419c-8d45-cc317dc132b7
my.str->7a89f88906daf2c83c0c5ee886b89ac1

 

命令行参数

SpringApplication转换参数要求以两个横杠开头(--),会添加到Spring Environment中

启动参数配置上添加如下内容

--cmd-param="I‘M CMD PARAM"

Runner.java 追加如下

System.out.println("------命令行参数-------");
String property8 = environment.getProperty("cmd-param");
System.out.println("cmd param ->"+property8);

输入如下内容:

------命令行参数-------
cmd param ->I‘M CMD PARAM

 

禁用命令行参数

package com.wm8.logging;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.setAddCommandLineProperties(false);
        springApplication.run(args);
    }
}

输出:

------命令行参数-------
cmd param ->null

 

应用属性文件

SpringBoot 默认加载属性文件 application.properties;会从一下位置查找文件

  • 当前目录的config子目录
  • 当前目录
  • 类路径下config包
  • 类路径

优先级从高到低(高优先级会覆盖低优先级的值)

创建application.yaml文件

项目根目录下application.yaml

file:
  location: "当前目录下" 

项目根目录的config目录下

file:
  location: "当前目录下的config子目录" 

项目resources目录下

file:
  location: "类路径" 

项目resources的子目录config目录下

file:
  location: "类路径的config目录"
  name: "来自类路径下的config目录"

Runner.java

package com.wm8.logging;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class Runner implements ApplicationRunner {
    
    @Autowired
    Environment environment;
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String property1 = environment.getProperty("file.location");
        System.out.println("file location ->"+property1);
        String property2 = environment.getProperty("file.name");
        System.out.println("file name ->"+property2);
    }

}

 

输出结果:

file location ->当前目录下的config子目录
file name ->来自类路径下的config目录

 

如果不喜欢用application.yaml这样的配置文件命名,可以通过spring.config.name来配置;位置也可以通过spring.config.location来指定

$ java -jar myproject.jar --spring.config.name=myproject
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
spring.config.name和spring.config.location必须很早进行定义。

 

SpringBoot 外部配置

标签:new   not   地方   package   其他   项目   code   查找文件   features   

原文地址:https://www.cnblogs.com/9wan/p/14239854.html

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