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 文件中设置 两个数值如下

  1. wen.hello=hello
  2. wen.world=world

写一个controller 通过value 注解 获取数据 如下

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.context.annotation.PropertySource;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * @author 作者 cxhc
  7. * @version 创建时间:2017年9月21日 下午10:05:49
  8. */
  9. @RestController
  10. @RequestMapping("/hello")
  11. public class HelloContreller {
  12. @Value("${wen.hello}")
  13. private String hello;
  14. @Value("${wen.world}")
  15. private String world;
  16. @RequestMapping("/index.do")
  17. public String helloPage() {
  18. System.out.println(hello+world);
  19. return hello+world;
  20. }
  21. }

打开浏览器输入 http://localhost:8080/hello/index.do 就能看到helloworld 如下 这里写图片描述 另外.properties 中德值也是可以互相引用的如创建一个helloworld 可以这么写 wen.helloworld=${wen.hello}${wen.world}

下面通过java 配置的方式配置 读写 properties 首先声明一个配置类

  1. /**
  2. * @author 作者 cxhc
  3. * @version 创建时间:2017年9月24日 下午6:14:25
  4. */
  5. //这里标注 前缀
  6. @Component
  7. @ConfigurationProperties(prefix = "wen")
  8. public class ConfigString {
  9. private String hello;
  10. private String world;
  11. /**
  12. * @return the hello
  13. */
  14. public String getHello() {
  15. return hello;
  16. }
  17. /**
  18. * @param hello the hello to set
  19. */
  20. public void setHello(String hello) {
  21. this.hello = hello;
  22. }
  23. /**
  24. * @return the world
  25. */
  26. public String getWorld() {
  27. return world;
  28. }
  29. /**
  30. * @param world the world to set
  31. */
  32. public void setWorld(String world) {
  33. this.world = world;
  34. }
  35. }

启动项目 便可以看到和之前一样的效果ConfigString 中德值也是可以修改的使用 ConfigString.setHello("haha");修改。

二.yml 配置

下面看yml 的配置 在properties 文件同一目录下创建一个application.yml的文件 内容如下

  1. wen:
  2. hello: hello
  3. world: world
  4. helloworld: ${wen.hello}${wen.world}

使用的方式和上面properties 是一样的就不再详细叙述了 注意一点 :冒号后要加个空格

三 配置优先级

.Application属性文件,按优先级排序,位置高的将覆盖位置低的

  1. 当前目录下的一个/config子目录
  2. 当前目录
  3. 一个classpath下的/config包
  4. classpath根路径(root) 同时properties 文件的优先级高于yml 另外springboot 中配置这些方式优先级如下:
  5. 命令行参数 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 同目录 内容如下

  1. com.wen.name=cxhc
  2. com.wen.age=22

创建一个实体类和他对应

  1. @Configuration
  2. @PropertySource(value = "classpath:test.properties")
  3. @ConfigurationProperties(prefix = "com.wen")
  4. public class TestConfig {
  5. private String name;
  6. private int age;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. public void setAge(int age) {
  17. this.age = age;
  18. }
  19. }

修改controller 添加一个方法获取test.properties 中的文件

  1. @RestController
  2. @RequestMapping("/hello")
  3. public class HelloContreller {
  4. @Autowired
  5. ConfigString ConfigString;
  6. @Autowired
  7. TestConfig testConfig;
  8. @RequestMapping("/index.do")
  9. public String helloPage() {
  10. ConfigString.setHello("haha");
  11. System.out.println(ConfigString.getHello()+ConfigString.getWorld());
  12. return ConfigString.getHello()+ConfigString.getWorld();
  13. }
  14. @RequestMapping("/testConfig.do")
  15. public String testConfig() {
  16. ConfigString.setHello("haha");
  17. System.out.println(testConfig.getName()+testConfig.getAge());
  18. return testConfig.getName()+testConfig.getAge();
  19. }
  20. }

启动项目浏览器输入http://localhost:8080/hello/testConfig.do便可以看到结果

五 多个环境配置文件

在开发过程一般都会遇到多环境配置的问题如开发环境,测试环境,生产环境等等。如果只用一个配置文件修改起来麻烦了 创建三个properties

  1. application-test.properties:测试环境 2.application-dev.properties:开发环境 3.application-prod.properties:生产环境 那怎么使用呢 例如现在项目工程使用的是默认的端口 8080 在 application-test.properties 添加端口配置改为 8081
  1. 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配置文件详解

posted on 2018-03-27 09:35  cxhc  阅读(190)  评论(0)    收藏  举报

导航