032_SpringBoot多环境属性配置文件
1. 语法结构: application-{profile}.properties。profile: 代表的就是一个环境变量。
2. 多环境属性配置文件例子
2.1. 使用maven构建SpringBoot的名叫spring-boot-multi-applicationproperties项目
2.2. 新建pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-applicationproperties-variable</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version></properties><dependencies><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><!-- 1. 在SpringBoot项目中, 框架提供了一套自己的打包机制, 是通过spring-boot-maven-plugin插件来实现的。可以在MAVEN的生命周期package后重新打包, 生成新的jar包。 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
2.3. 在src/main/resources下, 新建application-dev.properties, 开发环境使用。
server.port=7777
msg=dev env
2.4. 在src/main/resources下, 新建application-prod.properties, 生产环境使用。
server.port=8888
msg=prod env
2.5. 在src/main/resources下, 新建application-test.properties, 测试环境使用。
server.port=9999
msg=test env
2.6. 新建ConfigController.java
package com.bjbs.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigController {@Value("${msg}")private String msg;@RequestMapping("/showMsg")public String showMsg() {return msg;}
}
2.7. 新建App.java
package com.bjbs;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** spring Boot启动器*/
@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}
2.8. 使用Maven打包
2.9. 打包成功
2.10. target目录下生成jar
2.11. 拷贝jar到D盘, 使用命令测试开发环境:
java -jar spring-boot-applicationproperties-variable-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
2.12. 使用命令测试生产环境:
java -jar spring-boot-applicationproperties-variable-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
2.13. 使用命令测试测试环境:
java -jar spring-boot-applicationproperties-variable-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
032_SpringBoot多环境属性配置文件相关推荐
- 属性配置文件详解(2)(十七)
过命令行设置属性值 相信使用过一段时间Spring Boot的用户,一定知道这条命令:java -jar xxx.jar --server.port=8888,通过使用–server.port属性来设 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (二)Spring Boot属性配置文件详解...
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...
- Spring Boot属性配置文件详解
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...
- (拿来主义-8) Spring Boot属性配置文件详解(三)
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...
- Spring的properties属性配置文件和Spring常用注解
Spring的properties属性配置文件 问题: 在学习了Spring的知识后,需要被Spring管理的资源都是通过配置文件的形式来告诉Spring容器对象管理谁以及如何管理.但是随着使用次数的 ...
- Spring Boot教程(十七)属性配置文件详解(2)
通过命令行设置属性值 相信使用过一段时间Spring Boot的用户,一定知道这条命令:java -jar xxx.jar --server.port=8888,通过使用–server.port属性来 ...
- spring Boot 2 运维篇(spring boot程序的打包与部署,多环境开发配置文件的配置,spring boot集成日志框架)
目录 1.SpringBoot程序的打包与运行 程序打包 程序运行 SpringBoot程序打包失败处理 命令行启动常见问题及解决方案 SpringBoot项目快速启动(Linux版) 2.配置高级 ...
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三)属性配置文件详解(1)
2019独角兽企业重金招聘Python工程师标准>>> 相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring ...
- 解决Spring boot中读取属性配置文件出现中文乱码的问题
解决Spring boot中读取属性配置文件出现中文乱码的问题 参考文章: (1)解决Spring boot中读取属性配置文件出现中文乱码的问题 (2)https://www.cnblogs.com/ ...
最新文章
- 流数据分析平台Storm简介
- Win10 新版又悲剧了!老外神评论
- SharePoint 2010 产品六大功能模块
- 异常php有必要吗,关于php异常的问题,到底有什么好处?
- Eclipse is running in a JRE, but a JDK is required
- centos7启动root账户_新手小白使用vm安装Linux系统centos7虚拟机详细教程
- GdiPlus[29]: IGPPen: 虚线样式
- 判断一个单链表是否有环及环的链接点
- github issue 搜索_启用 GitHub Issue 作为博客留言系统 - Farseerfc的小窝
- 音频处理之语音加速播放
- 系统性学习计算机(一)
- 微信小程序实现登录功能
- MATLAB安装摄像头插件
- 单元测试系列一-为什么要写单元测试,何时写,写多细
- AutoCAD系统变量和环境变量
- FBX SDK下载安装教程
- 枚举,随机数模型,函数功能分解---扔骰子随机数游戏
- 我理解的Java栈与堆,String类
- Proxifier v3.15
- 整数转罗马数字(C++)