打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
spring boot中多环境配置支持
userphoto

2017.10.25 广东

关注

一、说明

    在我们的日常开发中,生产环境的配置和测试环境的配置以及开发环境的配置基本上都是不相同的,每次到部署环境的时候,就需要手动的切换配置文件,如果在切换的过程中一不小心的话,就会出错,所以在开发中,一般会搞个配置文件检查的功能,来避免出错,而spring boot则充分考虑了这种情况,为开发人员提供了天然的多环境配置支持。

二、增加properties配置文件

1、application-dev.properties

  1. # 开发环境的部署端口  
  2. server.port=7980  
  3. # 自定义属性  
  4. com.chhliu.springboot.author=chhliu  
  5. # 参数间引用  
  6. com.chhliu.springboot.age=${com.chhliu.age}  
  7. com.chhliu.springboot.sex=man  
  8. com.chhliu.springboot.time=20170123  
  9. com.chhliu.age=28  
2、application-prod.properties

  1. server.port=7982  
  2. com.chhliu.springboot.author=xyh  
  3. com.chhliu.springboot.age=${com.chhliu.age}  
  4. com.chhliu.springboot.sex=woman  
  5. com.chhliu.springboot.time=20170123  
  6. com.chhliu.age=27  
3、application-test.properties

  1. server.port=7984  
  2. com.chhliu.springboot.author=chhliuxyh  
  3. com.chhliu.springboot.age=${com.chhliu.age}  
  4. com.chhliu.springboot.sex=woman  
  5. com.chhliu.springboot.time=20170123  
  6. com.chhliu.age=24  
4、application-BEIJING.properties
  1. server.port=7981  
只要配置文件的命名满足application-{profile}.properties格式就行

三、指定具体使用哪个配置

application.properties

  1. spring.profiles.active=prod // 指定具体使用哪种配置环境,此处指定使用application-prod.properties配置文件中的环境  
四、新建自定义属性对应的实体类

  1. package com.chhliu.springboot.properties.config;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4.   
  5. /** 
  6.  * @author chhliu 
  7.  * 自定义属性对应的实体类 
  8.  * spring boot会将配置文件中自定义的属性值,自动设置到该类对应的属性上,使用的使用直接注入该类即可 
  9.  * prefix用来指定自定义属性值的前缀 
  10.  */  
  11. @ConfigurationProperties(prefix="com.chhliu.springboot")  
  12. public class ConfigProperties {  
  13.     private String author;  
  14.     private int age;  
  15.     private String sex;  
  16.     private String time;  
  17.       
  18.         ……省略getter,setter方法……  
  19.     @Override  
  20.     public String toString() {  
  21.         return "ConfigProperties [author=" + author + ", age=" + age + ", sex=" + sex + ", time=" + time + "]";  
  22.     }  
  23. }  
五、新建一个业务接口

  1. package com.chhliu.springboot.properties.service;  
  2.   
  3. public interface PropertiesEnv {  
  4.     String getPropertiesEnv();  
  5. }  
六、接口实现类

1、实现类一

  1. package com.chhliu.springboot.properties.service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.annotation.Profile;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.chhliu.springboot.properties.config.ConfigProperties;  
  8.   
  9. /** 
  10.  * @author chhliu 
  11.  * 其中@Profile用来指定工作环境,例如示例中为“dev”,那么该类只会在配置文件为“dev”的环境下,才会调用该类 
  12.  */  
  13. @Service("devPropertiesEnv")  
  14. @Profile("dev") // 当配置为开发环境时,生效  
  15. public class DevPropertiesEnv implements PropertiesEnv{  
  16.     @Autowired // 注入自定义属性对应的实体类  
  17.     private ConfigProperties properties;  
  18.     @Override  
  19.     public String getPropertiesEnv() {  
  20.         return properties.toString();  
  21.     }  
  22. }  
2、实现类二

  1. package com.chhliu.springboot.properties.service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.annotation.Profile;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.chhliu.springboot.properties.config.ConfigProperties;  
  8.   
  9. /** 
  10.  * @author chhliu 
  11.  * 其中@Profile用来指定工作环境,例如示例中为“prod”,那么该类只会在配置文件为“prod”的环境下,才会调用该类 
  12.  */  
  13. @Service("proPropertiesEnv")  
  14. @Profile("prod")// 当配置为生产环境时,生效  
  15. public class ProPropertiesEnv implements PropertiesEnv {  
  16.   
  17.     @Autowired  
  18.     private ConfigProperties properties;  
  19.     @Override  
  20.     public String getPropertiesEnv() {  
  21.         return properties.toString();  
  22.     }  
  23. }  
七、开启支持

  1. package com.chhliu.springboot.properties;  
  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.chhliu.springboot.properties.config.ConfigProperties;  
  8.   
  9. @SpringBootApplication  
  10. @EnableConfigurationProperties({ConfigProperties.class}) // 开启配置属性支持  
  11. public class SpringbootPropertiesApplication {  
  12.   
  13.     public static void main(String[] args) {  
  14.         SpringApplication.run(SpringbootPropertiesApplication.class, args);  
  15.     }  
  16. }  
八、controller编写

  1. package com.chhliu.springboot.properties.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.web.bind.annotation.GetMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. import com.chhliu.springboot.properties.service.PropertiesEnv;  
  8.   
  9. @RestController  
  10. public class HelloWorld {  
  11.       
  12.     @Autowired // 注入接口服务  
  13.     private PropertiesEnv env;  
  14.       
  15.     @GetMapping("/hello")  
  16.     public String sayHello(){  
  17.         return "world";  
  18.     }  
  19.       
  20.     @GetMapping("properties")  
  21.     public String getPropertiesEnv(){  
  22.         return env.getPropertiesEnv();  
  23.     }  
  24. }  
九、测试

Tomcat输出

  1. Tomcat started on port(s): 7982 (http)  
发现,此时的服务端口为7982,而该端口对应的正式prod环境配置

在浏览器中输入测试url:http://localhost:7982/properties

  1. ConfigProperties [author=xyh, age=27, sex=woman, time=20170123]  
测试结果也对应。

总结:这种多环境配置在其他的应用场景中也是非常有用的,比如说,测试的时候,不希望调某个接口,不希望给某人发邮件等等,就可以用多环境配置进行区分!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SpringBoot之两种配置文件properties与yml文件的区别及读取方式
SpringBoot源码解析-自定义Starter
Spring: (一) -- 春雨润物之 核心IOC
java中properties配置文件的用法
2020最新总结,大厂常问的SpringBoot高频面试题(30题+答案+学习导图)
【微服务】131:最好用的框架SpringBoot
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服