标签: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}
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;会从一下位置查找文件
优先级从高到低(高优先级会覆盖低优先级的值)
创建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必须很早进行定义。
标签:new not 地方 package 其他 项目 code 查找文件 features
原文地址:https://www.cnblogs.com/9wan/p/14239854.html