Spring Boot – Profile配置详解编程语言
2023-06-13 09:20:46 时间
Profile是什么
Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。
Profile使用假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。
基于properties文件类型你可以另外建立3个环境下的配置文件:
applcation.properties application-dev.properties application-test.properties application-prod.properties
然后在applcation.properties文件中指定当前的环境:
spring.profiles.active=test 这时候读取的就是application-test.properties文件。
只需要一个applcation.yml文件就能搞定,推荐此方式。
spring: profiles: active: prod spring: profiles: dev server: port: 8080 spring: profiles: test server: port: 8081 spring.profiles: prod spring.profiles.include: - proddb - prodmq server: port: 8082 spring: profiles: proddb name: mysql spring: profiles: prodmq address: localhost
此时读取的就是prod的配置,prod包含proddb,prodmq,此时可以读取proddb,prodmq下的配置。
也可以同时激活三个配置。
spring.profiles.active: prod,proddb,prodmq基于Java代码
在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@[email protected]@Component注解。
@Configuration @Profile("prod") public class ProductionConfiguration { // ... }指定Profile main方法启动方式:
// 在Eclipse Arguments里面添加 --spring.profiles.active=prod插件启动方式:
spring-boot:run -Drun.profiles=prodjar运行方式:
java -jar xx.jar --spring.profiles.active=prod
除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles方法。
SpringApplication.class
public void setAdditionalProfiles(String... profiles) { this.additionalProfiles = new LinkedHashSet String (Arrays.asList(profiles)); }
推荐:Spring Boot Cloud 最强技术教程
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16258.html
cjavamysql相关文章
- 【Spring Boot 20,孙鑫Java视频教程百度网盘
- Spring Boot拦截器配置拦截登陆「建议收藏」
- 利用 Spring Boot Admin 对 Spring Boot 应用监控以及配置认证
- Spring Boot、Spring Cloud 自定义配置文件(如何整合配置中心)
- spring boot的基本配置——spring boot的web开发——Thymeleaf模板引擎———通过HttpSession session的session.setAttribute返回数据给
- Spring-boot_Spring Boot
- 不会前端没事,用GWT Boot和Spring Boot构建Web程序
- Spring boot串口通信,windows、linux,docker环境配置说明
- 【FS-Blog】个人博客,Spring Boot 开山之作
- 【Spring boot Nacos系列教程】Nacos第三篇:多环境配置
- [Redis]Spring Boot中Redis Template集群配置
- 全面学习MongoDB,在Spring Boot项目中整合 MongoDB
- Spring Boot 2.6之后,动态权限控制终于可以用起来了!
- Spring Boot 2.4版本前后的分组配置变化及对多环境配置结构的影响
- Spring Boot的核心注解和配置(一)
- Spring Boot的数据源配置
- Spring Boot的安全配置(三)
- Spring Boot的监控
- Spring Boot 2.x基础教程:使用Flyway管理数据库版本
- Linux启动与停止spring boot工程的脚本示例
- Spring Boot(六): Favicon配置详解编程语言
- Spring Boot全局支持CORS(跨源请求)的配置方法详解编程语言
- spring Boot(十九):使用Spring Boot Actuator监控应用详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- spring boot详解编程语言
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- Spring Boot 2.0 配置图文教程