目录
一、⾃定义属性与加载
方式一
方式二
注意:
二、参数间的引⽤
三、使⽤随机数
四、通过命令⾏设置属性值
五、多环境配置
如下总结多环境的配置思路:
相信很多⼈选择Spring Boot主要是考虑到它既能兼顾Spring的强⼤功能,还能实现快速开发的便捷。我们在Spring Boot使⽤过程中,最直观的感受就是没有了原来⾃⼰整合Spring应⽤时繁多的XML配置内容,替代它的是在 pom.xml 中引⼊模块化的 Starter POMs ,其中各个模块都有⾃⼰的默认配置,所以如果不是特殊应⽤场景,就只需要在 application.properties 中完成⼀些属性配置就能开启各模块的应⽤。
我们在使⽤Spring Boot的时候,通常也需要定义⼀些⾃⼰使⽤的属性,我们可以如下⽅式直接定义:
com.didispace.blog.name=小明
com.didispace.blog.title=SpringBoot配置文件
通过 @Value("${属性名}") 注解来加载对应的配置属性,具体如下:
@Component
public class BlogProperties {
@Value("${com.didispace.blog.name}")
private String name;
@Value("${com.didispace.blog.title}")
private String title;
// 省略getter和setter
}
通过 @ConfigurationProperties(prefix = "com.didispace.blog”) 注解,将配置⽂件中以 home 前缀的属性值⾃动绑定到对应的字段中。同时⽤ @Component 作为 Bean 注⼊到 Spring 容器中。
@Component
@ConfigurationProperties(prefix = "com.didispace.blog”)
public class BlogProperties {
private String name;
private String title;
// 省略getter和setter
}
1、application.properties 配置中⽂值的时候,读取出来的属性值会出现乱码问题。但是 application.yml不会出现乱码问题。原因是,Spring Boot 是以 iso-8859 的编码⽅式读取 application.properties 配置⽂件。
2、如果定义⼀个键值对 user.name=xxx ,这⾥会读取不到对应写的属性值。为什么呢?Spring Boot 的默认 StandardEnvironment ⾸先将会加载 “systemEnvironment" 作为⾸个PropertySource. ⽽ source 即为System.getProperties().当 getProperty时,按照读取顺序,返回 “systemEnvironment" 的值.即System.getProperty("user.name")
user.name=lisi
@SpringBootTest
public class SpringBootWebTest {
@Value("${user.name}")
private String name;
@Test
public void test(){
System.out.println(name);
}
}
通过测试会发现,返回值是你windows的用户名
在 application.properties 中的各个参数之间也可以直接引⽤来使⽤,就像下⾯的设置:
com.didispace.blog.name=小明
com.didispace.blog.title=SpringBoot配置文件
com.didispace.blog.desc=${com.didispace.blog.name}正在努⼒写《${com.didispace.blog.title}》
com.didispace.blog.desc 参数引⽤了上⽂中定义的 name 和 title 属性,最后该属性的值就是
小明正在努⼒写《SpringBoot配置文件》 。
在⼀些情况下,有些参数我们需要希望它不是⼀个固定的值,⽐如密钥、服务端⼝等。SpringBoot的属性配置⽂件中可以通过 ${random} 来产⽣int值、long值或者string字符串,来⽀持属性的随机值。
# 随机字符串
com.didispace.blog.value=${random.value}
# 随机int
com.didispace.blog.number=${random.int}
# 随机long
com.didispace.blog.bignumber=${random.long}
# 10以内的随机数
com.didispace.blog.test1=${random.int(10)}
# 10-20的随机数
com.didispace.blog.test2=${random.int[10,20]}
相信使⽤过⼀段时间Spring Boot的⽤户,⼀定知道这条命令: java -jar xxx.jar --server.port=8888 ,通过使⽤—server.port属性来设置xxx.jar应⽤的端⼝为8888。
注意:打包插件应该选用SpringBoot的打包插件,不然会出现一些问题
org.springframework.boot
spring-boot-maven-plugin
具体查看下面的文章
(4条消息) spring-boot-01-helloworld-1.0-SNAPSHOT.jar中没有主清单属性【解决方案】_小鲁蛋儿的博客-CSDN博客
在命令⾏运⾏时,连续的两个减号 -- 就是对 application.properties 中的属性值进⾏赋值的标识。
所以, java -jar xxx.jar --server.port=8888 命令,等价于我们在 application.properties 中添加
属性 server.port=8888。
通过命令⾏来修改属性值固然提供了不错的便利性,但是通过命令⾏就能更改应⽤运⾏的参数,那岂不是很不安全?是的,所以Spring Boot也贴⼼的提供了屏蔽命令⾏访问属性的设置,只需要这句设置就能屏蔽: SpringApplication.setAddCommandLineProperties(false)。
具体如下:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.setAddCommandLineProperties(false);
springApplication.run(args);
}
}
我们在开发Spring Boot应⽤时,通常同⼀套程序会被应⽤和安装到⼏个不同的环境,⽐如:开发、测试、⽣产等。其中每个环境的数据库地址、服务器端⼝等等配置都会不同,如果在为不同环境打包时都要频繁修改配置⽂件的话,那必将是个⾮常繁琐且容易发⽣错误的事。
对于多环境的配置,各种项⽬构建⼯具或是框架的基本思路是⼀致的,通过配置多份不同环境的配置⽂件,再通过打包命令指定需要打包的内容之后进⾏区分打包,Spring Boot也不例外,或者说更加简单。
在Spring Boot中多环境配置⽂件名需要满⾜ application-{profile}.properties 的格式,其
中 {profile} 对应你的环境标识,⽐如:
⾄于哪个具体的配置⽂件会被加载,需要在 application.properties ⽂件中通过 spring.profiles.active 属性来设置,其值对应 {profile} 值。
如: spring.profiles.active=test 就会加载 application-test.properties 配置⽂件内容
参考文章:Spring For All 玩最纯粹的技术!做最专业的 Spring 民间组织~
泥瓦匠BYSocket