Spring Boot学习之旅:(二)两种配置文件
上一篇中创建了一个简单的springboot工程,简单的讲述了一下springboot 的启动方式,这里补充一下 ,除了从启动类启动外,还可以通过maven 用命令 maven package 打包成一个jar包的方式通过 命令行 java -jar 工程名.jar 启动 或者通过maven 插件 springboot:run 的方式 咳咳再将下去就跑题了。 下面进入正题这篇文章主要讲 springboot 的两种配置文件 .properties .yml 以及各种方式的优先级别。 springboot采纳了建立生产就绪Spring应用程序的观点。 Spring Boot优先于配置的惯例,旨在让您尽快启动和运行。在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行。在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性。
一 .properties 配置
先看resources 文件夹下面有没有application.propertis 文件 没有的话创建一个。设置一下文件的编码。设置为utf-8在application.properties 文件中设置 两个数值如下
wen.hello=hellowen.world=world
写一个controller 通过value 注解 获取数据 如下
import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @author 作者 cxhc* @version 创建时间:2017年9月21日 下午10:05:49*/@RestController@RequestMapping("/hello")public class HelloContreller {@Value("${wen.hello}")private String hello;@Value("${wen.world}")private String world;@RequestMapping("/index.do")public String helloPage() {System.out.println(hello+world);return hello+world;}}
打开浏览器输入 http://localhost:8080/hello/index.do 就能看到helloworld 如下 另外.properties 中德值也是可以互相引用的如创建一个helloworld 可以这么写 wen.helloworld=${wen.hello}${wen.world}
下面通过java 配置的方式配置 读写 properties 首先声明一个配置类
/*** @author 作者 cxhc* @version 创建时间:2017年9月24日 下午6:14:25*///这里标注 前缀@Component@ConfigurationProperties(prefix = "wen")public class ConfigString {private String hello;private String world;/*** @return the hello*/public String getHello() {return hello;}/*** @param hello the hello to set*/public void setHello(String hello) {this.hello = hello;}/*** @return the world*/public String getWorld() {return world;}/*** @param world the world to set*/public void setWorld(String world) {this.world = world;}}
启动项目 便可以看到和之前一样的效果ConfigString 中德值也是可以修改的使用 ConfigString.setHello("haha");修改。
二.yml 配置
下面看yml 的配置 在properties 文件同一目录下创建一个application.yml的文件 内容如下
wen:hello: helloworld: worldhelloworld: ${wen.hello}${wen.world}
使用的方式和上面properties 是一样的就不再详细叙述了 注意一点 :冒号后要加个空格
三 配置优先级
.Application属性文件,按优先级排序,位置高的将覆盖位置低的
- 当前目录下的一个/config子目录
- 当前目录
- 一个classpath下的/config包
- classpath根路径(root) 同时properties 文件的优先级高于yml 另外springboot 中配置这些方式优先级如下:
- 命令行参数 2.来自java:comp/env的JNDI属性 3.Java系统属性(System.getProperties()) 4.操作系统环境变量 5.RandomValuePropertySource配置的random.*属性值 6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件 7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件 8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件 9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件 10.@Configuration注解类上的@PropertySource 11.通过SpringApplication.setDefaultProperties指定的默认属性
四.自定义配置文件
上面介绍的是我们都把配置文件写到application.yml或者application.properties中。有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如test.properties: 创建一个test.properties 文件和application.properties 同目录 内容如下
com.wen.name=cxhccom.wen.age=22
创建一个实体类和他对应
@Configuration@PropertySource(value = "classpath:test.properties")@ConfigurationProperties(prefix = "com.wen")public class TestConfig {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
修改controller 添加一个方法获取test.properties 中的文件
@RestController@RequestMapping("/hello")public class HelloContreller {@AutowiredConfigString ConfigString;@AutowiredTestConfig testConfig;@RequestMapping("/index.do")public String helloPage() {ConfigString.setHello("haha");System.out.println(ConfigString.getHello()+ConfigString.getWorld());return ConfigString.getHello()+ConfigString.getWorld();}@RequestMapping("/testConfig.do")public String testConfig() {ConfigString.setHello("haha");System.out.println(testConfig.getName()+testConfig.getAge());return testConfig.getName()+testConfig.getAge();}}
启动项目浏览器输入http://localhost:8080/hello/testConfig.do便可以看到结果
五 多个环境配置文件
在开发过程一般都会遇到多环境配置的问题如开发环境,测试环境,生产环境等等。如果只用一个配置文件修改起来麻烦了 创建三个properties
- application-test.properties:测试环境 2.application-dev.properties:开发环境 3.application-prod.properties:生产环境 那怎么使用呢 例如现在项目工程使用的是默认的端口 8080 在 application-test.properties 添加端口配置改为 8081
server.port=8081
在application.properties 中引用 application-test.properties 添加 spring.profiles.active=test 启动项目 发现端口已经修改过来了
六推荐
项目源码 https://github.com/haha174/boot
个人博客 http://www.haha174.top/article/details/259872
七 查考文献
spring-boot-reference-guide-zh pring Boot干货系列:(二)配置文件解析 Spring Boot 属性配置和使用 SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解
浙公网安备 33010602011771号