前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/

正文

Spring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下。Sping Boot的全局配置文件的作用是对一些默认配置的配置值进行修改

注:如果你的工程没有这个application.properties,那就在src/main/resources目录下新建一个。

自定义属性

application.properties提供自定义属性的支持,这样我们就可以把一些常量配置在这里:

1 com.birdboot.bootDemo.author=one-bird
2 com.birdboot.bootDemo.dream=world peace

然后直接在要使用的地方通过注解@Value(value=”${config.name}”)就可以绑定到想要的属性上面

 1 package com.birdboot.bootDemo;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 @SpringBootApplication
11 public class BootDemoApplication {
12     
13     @Value("${com.birdboot.bootDemo.author}")
14     private String name;
15     
16     @Value("${com.birdboot.bootDemo.dream}")
17     private String dream;
18 
19     @RequestMapping("/")
20     public String index(){
21         return name + " " + dream;
22     }
23     
24     public static void main(String[] args) {
25         SpringApplication.run(BootDemoApplication.class, args);
26     }
27 
28 }

我们启动工程输入http://localhost:8080 就可以看到打印了”one-bird world peace”。

有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个ConfigBean.java类,顶部需要使用注解@ConfigurationProperties(prefix = “com.birdboot.bootDemo”)来指明使用哪个

 1 package com.birdboot.module.bean;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 
 5 @ConfigurationProperties(prefix = "com.birdboot.bootDemo")
 6 public class ConfigBean {
 7 
 8     private String name;
 9     private String dream;
10     
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getDream() {
18         return dream;
19     }
20     public void setDream(String dream) {
21         this.dream = dream;
22     }
23 }

这里配置完还需要在spring Boot入口类加上@EnableConfigurationProperties并指明要加载哪个bean,如果不写ConfigBean.class,在bean类那边添加

 1 package com.birdboot.bootDemo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 6 
 7 import com.birdboot.module.bean.ConfigBean;
 8 
 9 @SpringBootApplication
10 @EnableConfigurationProperties({ConfigBean.class})
11 public class BootDemo2Application {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(BootDemo2Application.class, args);
15     }
16 }

最后在Controller中引入ConfigBean使用即可,如下:

 1 package com.birdboot.module.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import com.birdboot.module.bean.ConfigBean;
 8 
 9 @RestController
10 public class UserController {
11 
12     @Autowired
13     private ConfigBean configBean;
14     
15     @RequestMapping("/")
16     public String hexo(){
17         return configBean.getName() + configBean.getDream();
18     }
19 }

参数间引用

在application.properties中的各个参数之间也可以直接引用来使用,如下面的设置:

1 com.birdboot.bootDemo.author=one-bird
2 com.birdboot.bootDemo.dream=world peace
3 com.birdboot.bootDemo.line=${com.birdboot.bootDemo.author} hope ${com.birdboot.bootDemo.dream}

这样我们就可以只是用line这个属性就好

使用自定义配置文件

有时候不希望把所有配置都放在application.properties里面,这时候我们可以另外定义一个,这里命名为test.properties,路径跟也放在src/main/resources下面

1 com.birdboot.bootDemo.author=bird
2 com.birdboot.bootDemo.dream=house

新建一个bean类,如下:

 1 package com.birdboot.module.bean;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.context.annotation.PropertySource;
 6 
 7 @Configuration
 8 @ConfigurationProperties(prefix = "com.birdboot.bootDemo")
 9 @PropertySource("classpath:test.properties")
10 public class Config2Bean {
11 
12     private String name;
13     private String dream;
14     
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public String getDream() {
22         return dream;
23     }
24     public void setDream(String dream) {
25         this.dream = dream;
26     }
27 }

这里要注意哦,有一个问题,如果你使用的是1.5以前的版本,那么可以通过locations指定properties文件的位置,这样:

1 @ConfigurationProperties(prefix = "com.birdboot.bootDemo",locations="classpath:test.properties")

但是1.5版本后就没有这个属性了,找了半天发现添加@Configuration和@PropertySource(“classpath:test.properties”)后才可以读取。

随机值配置

配置文件中${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦,例如 生成 int 值、long 值或者 string 字符串。

1 bird.secret=${random.value}
2 bird.number=${random.int}
3 bird.bignumber=${random.long}
4 bird.uuid=${random.uuid}
5 bird.number.less.than.ten=${random.int(10)}
6 bird.number.in.range=${random.int[1024,65536]}

外部配置-命令行参数配置

Spring Boot是基于jar包运行的,打成jar包的程序可以直接通过下面命令运行:

1 java -jar xx.jar

可以以下命令修改tomcat端口号:

1 java -jar xx.jar --server.port=9090

 可以看出,命令行中连续的两个减号--就是对application.properties中的属性值进行赋值的标识。

所以java -jar xx.jar --server.port=9090等价于在application.properties中添加属性server.port=9090
如果你怕命令行有风险,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它。

实际上,Spring Boot应用程序有多种设置途径,Spring Boot能从多重属性源获得属性,包括如下几种:

  • 根目录下的开发工具全局设置属性(当开发工具激活时为~/.spring-boot-devtools.properties)。
  • 测试中的@TestPropertySource注解。
  • 测试中的@SpringBootTest#properties注解特性。
  • 命令行参数
  • SPRING_APPLICATION_JSON中的属性(环境变量或系统属性中的内联JSON嵌入)。
  • ServletConfig初始化参数。
  • ServletContext初始化参数。
  • java:comp/env里的JNDI属性
  • JVM系统属性
  • 操作系统环境变量
  • 随机生成的带random.* 前缀的属性(在设置其他属性时,可以应用他们,比如${random.long})
  • 应用程序以外的application.properties或者appliaction.yml文件
  • 打包在应用程序内的application.properties或者appliaction.yml文件
  • 通过@PropertySource标注的属性源
  • 默认属性(通过SpringApplication.setDefaultProperties指定).

这里列表按组优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性,列如我们上面提到的命令行属性就覆盖了application.properties的属性。

配置文件的优先级

application.properties和application.yml文件可以放在以下四个位置:

  • 外置,在相对于应用程序运行目录的/congfig子目录里。
  • 外置,在应用程序运行的目录里
  • 内置,在config包内
  • 内置,在Classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,如图:

 

 

 此外,如果在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml。

Profile-多环境配置

当应用程序需要部署到不同运行环境时,一些配置细节通常会有所不同,最简单的比如日志,生产日志会将日志级别设置为WARN或更高级别,并将日志写入日志文件,而开发的时候需要日志级别为DEBUG,日志输出到控制台即可。
如果按照以前的做法,就是每次发布的时候替换掉配置文件,这样太麻烦了,Spring Boot的Profile就给我们提供了解决方案,命令带上参数就搞定。

这里我们来模拟一下,只是简单的修改端口来测试
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-prod.properties:生产环境

想要使用对应的环境,只需要在application.properties中使用spring.profiles.active属性来设置,值对应上面提到的{profile},这里就是指dev、prod这2个。
当然你也可以用命令行启动的时候带上参数:

1 java -jar xxx.jar --spring.profiles.active=dev

给不同的环境添加不同的端口属性server.port,然后根据指定不同的spring.profiles.active来切换使用。

除了可以用profile的配置文件来分区配置我们的环境变量,在代码里,我们还可以直接用@Profile注解来进行配置,例如数据库配置,这里我们先定义一个接口。

1 package com.birdboot.module.service;
2 
3 public interface DBConnector {
4 
5     public void configure();
6 }

分别定义俩个实现类来实现它

 1 package com.birdboot.module.service.impl;
 2 
 3 import org.springframework.context.annotation.Profile;
 4 import org.springframework.stereotype.Component;
 5 
 6 import com.birdboot.module.service.DBConnector;
 7 
 8 /**
 9  * 测试环境
10  * @author one-bird
11  */
12 @Component
13 @Profile("testdb")
14 public class TestDBConnector implements DBConnector {
15 
16     @Override
17     public void configure() {
18         System.out.println("testdb");
19     }
20 
21 }
 1 package com.birdboot.module.service.impl;
 2 
 3 import org.springframework.context.annotation.Profile;
 4 import org.springframework.stereotype.Component;
 5 
 6 import com.birdboot.module.service.DBConnector;
 7 
 8 /**
 9  * 开发环境
10  * @author one-bird
11  */
12 @Component
13 @Profile("devdb")
14 public class DevDBConnector implements DBConnector {
15 
16     @Override
17     public void configure() {
18         System.out.println("devdb");
19     }
20 
21 }

通过在配置文件激活具体使用哪个实现类

1 spring.profiles.active=testdb

然后就可以这么用了

 1 package com.birdboot.module.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import com.birdboot.module.service.DBConnector;
 8 
 9 @RestController
10 @RequestMapping("/task")
11 public class TaskController {
12 
13     @Autowired 
14     DBConnector connector;
15     
16     @RequestMapping(value = {"/",""})
17     public String hellTask(){
18         connector.configure(); //最终打印testdb
19         return "hello task !! myage is ";// + myage;
20     }
21 }

除了spring.profiles.active来激活一个或者多个profile之外,还可以用spring.profiles.include来叠加profile

1 spring.profiles.active: testdb  
2 spring.profiles.include: proddb,prodmq

总结

这次对Spring Boot中application.properties配置文件做的整理总结,希望对大家有所帮助,最后贴上Spring Boot中常用的配置属性,需要的时候可打开查找:

https://www.cnblogs.com/cainiaomahua/p/11609599.html

后记

 此处记录本人的一些踩过的坑。

1、Spring boot的启动类存放位置需要放置在controller类的父级文件夹,否则通过网页访问,会报404,如下图:

 

posted on 2019-09-29 19:33  菜鸟麻花  阅读(180)  评论(0编辑  收藏  举报