springboot【2】入门之配置文件
来源:互联网 发布:js 如何拖拽 编辑:程序博客网 时间:2024/05/21 15:37
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml
中引入模块化的Starter POMs
,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties
中完成一些属性配置就能开启各模块的应用。
配置文件application.properties
的使用,主要用来配置数据库连接、日志相关配置等。除了这些配置内容之外,本文将具体介绍一些在application.properties
配置中的其他特性和使用方法。
1.自定义属性与加载
我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义:
test.springboot.name=学习test.springboot.context=我
在application.properties
中的各个参数之间也可以直接引用来使用,就像下面的设置:
#参数间引用test.springboot.desc=${test.springboot.context}爱${test.springboot.name}
test.springboot.desc
参数引用了上文中定义的name
和context
属性,最后该属性的值就是:我爱学习
。引用值:通过@Value("${属性名}")
注解来加载对应的配置属性,具体如下:
package com.lyd.entity;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class TestProperties {@Value("${test.springboot.name}")private String name;@Value("${test.springboot.context}")private String context;@Value("${test.springboot.desc}")private String desc;//get/set方法...}
验证:通过单元测试来验证TestProperties中的属性是否已经根据配置文件加载了。
package com.lyd;import org.hibernate.validator.internal.util.privilegedactions.GetConstraintValidatorList;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.lyd.entity.TestProperties;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(Application.class)public class ApplicationTest {@Autowiredprivate TestProperties testProperties;@Testpublic void getHello(){Assert.assertEquals(testProperties.getName(), "学习");Assert.assertEquals(testProperties.getDesc(), "我爱学习");}}
2.使用随机数
在一些情况下,有些参数我们需要希望它不是一个固定的值,比如密钥、服务端口等。Spring Boot的属性配置文件中可以通过${random}
来产生int值、long值或者string字符串,来支持属性的随机值。
#随机数##随机字符串test.springboot.value=${random.value}##随机inttest.springboot.number=${random.int}##随机longtest.springboot.bignumber=${random.long}##10以内的随机数test.springboot.test1=${random.int(10)}##10-20的随机数test.springboot.test2=${random.int[10,20]}
引用值得方式同上TestProperties
单元测试进行验证:
package com.lyd;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.validator.internal.util.privilegedactions.GetConstraintValidatorList;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.lyd.entity.TestProperties;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(Application.class)public class ApplicationTest {private static final Log log = LogFactory.getLog(ApplicationTest.class);@Autowiredprivate TestProperties testProperties;@Testpublic void getHello(){Assert.assertEquals(testProperties.getName(), "学习");Assert.assertEquals(testProperties.getDesc(), "我爱学习");log.info("随机字符串测试:"+testProperties.getValue());}}
3.可通过命令行设置属性值
命令:java -jar xxx.jar --server.port=8080
,通过使用–server.port属性来设置xxx.jar应用的端口为8888。
在命令行运行时,连续的两个减号--
就是对application.properties
中的属性值进行赋值的标识。所以这个命令等价于我们在application.properties
中添加属性server.port=8080
通过命令行来修改属性值固然提供了不错的便利性,但是通过命令行就能更改应用运行的参数,是很不安全的,所以Spring Boot也提供了屏蔽命令行访问属性的设置,只需要这句设置就能屏蔽:SpringApplication.setAddCommandLineProperties(false)
。
4.多环境配置
在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。
对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties
的格式,其中{profile}
对应你的环境标识,比如:
application-dev.properties
:开发环境application-test.properties
:测试环境application-prod.properties
:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties
文件中通过spring.profiles.active
属性来设置,其值对应{profile}
值。
如:spring.profiles.active=test
就会加载application-test.properties
配置文件内容
下面,以不同环境配置不同的服务端口为例,进行样例实验。
针对各环境新建不同的配置文件
application-dev.properties
、application-test.properties
、application-prod.properties
在这三个文件均都设置不同的
server.port
属性,如:dev环境设置为1111,test环境设置为2222,prod环境设置为3333application.properties中设置
spring.profiles.active=dev
,就是说默认以dev环境设置测试不同配置的加载
- 执行
java -jar xxx.jar
,可以观察到服务端口被设置为1111
,也就是默认的开发环境(dev) - 执行
java -jar xxx.jar --spring.profiles.active=test
,可以观察到服务端口被设置为2222
,也就是测试环境的配置(test) - 执行
java -jar xxx.jar --spring.profiles.active=prod
,可以观察到服务端口被设置为3333
,也就是生产环境的配置(prod)
- 执行
代码:
(1)aplication.properties
#多环境配置文件激活属性spring.profiles.active=dev
(2)
# 服务端口server.port=1111
按照上面的实验,可以如下总结多环境的配置思路:
application.properties
中配置通用内容,并设置spring.profiles.active=dev
,以开发环境为默认配置application-{profile}.properties
中配置各个环境不同的内容- 通过命令行方式去激活不同环境的配置
下面是完整项目结构:
源码下载:example02
- springboot【2】入门之配置文件
- 【SpringBoot】SpringBoot之入门配置文件
- SpringBoot学习2之配置文件
- Springboot 之 多配置文件
- Springboot配置文件之properties
- Springboot配置文件之yaml
- SpringBoot---配置文件2
- 6.Springboot 之 多配置文件
- springboot实战之读取配置文件
- SpringBoot之属性配置文件详解
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- SpringBoot入门之HelloWorld
- SpringBoot入门之HelloWorld
- 使用postgre数据库实现树形结构表的子-父级迭代查询,通过级联菜单简单举例
- 列可以设置 :formatter,对列的值进行处理
- 分布式缓存Redis Centos下单节点安装
- struts框架
- postgresql----索引失效
- springboot【2】入门之配置文件
- HDFS之DataNode工作原理超全总结
- 91. Decode Ways
- 【学习笔记】使用Visual Studio Code来开发TypeScript的环境记事
- java properties操作
- 贴两个sql
- jquery 新开窗口 防止浏览器拦截
- mysql命令更新一个字段的值
- MyBatis中为类配置别名,列名与属性名不对应的解决方案