java - 从 spring 配置文件中删除其他配置文件

标签 java spring configuration spring-profiles

我有 application.yaml 配置文件,其中包含一些配置文件:

server:
  address: 0.0.0.0
  port: 9090
db:
  url: 'jdbc:postgresql://localhost:5432/the_db'
  driver: 'org.postgresql.Driver'
  username: 'postgres'
  password: ''
---
spring:
  profiles: devArtem
db:
  url: 'jdbc:postgresql://localhost:5432/my_db'
---
spring:
  profiles: prod_1
db:
  password: 'first_pass'
---
spring:
  profiles: prod_2
db:
  password: 'second_pass'

我想在构建 jar 文件之前删除其他配置文件。例如,我不想向 prod_2 平台授予 prod_1 密码的访问权限。

对于 prod_1 来说,它必须是这样的:

server:
  address: 0.0.0.0
  port: 9090
db:
  url: 'jdbc:postgresql://localhost:5432/the_db'
  driver: 'org.postgresql.Driver'
  username: 'postgres'
  password: 'first_pass'

或者这个:

server:
  address: 0.0.0.0
  port: 9090
db:
  url: 'jdbc:postgresql://localhost:5432/the_db'
  driver: 'org.postgresql.Driver'
  username: 'postgres'
  password: ''
---
spring:
  profiles: prod_1
db:
  password: 'first_pass'

最佳答案

您可以使用多个 application-{profile}.yml 并为每个团队提供正确的文件。

例如:

application-devArtem.yml ---> to team devArtem
application-prod_1.yml ---> to team prod_1
application-prod_2.yml ---> to team prod_2

关于java - 从 spring 配置文件中删除其他配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899468/

相关文章:

java - 如何映射 url 中的项目列表 RequestMapping

gradle - build.gradle 配置中的星号 '*' 是什么意思?

c# - 具有非 ConfigurationElement 属性的自定义配置部分

Java : read and write a file together

java - 将多个 JTable 作为一个作业打印——Book 对象仅打印第一个表

java - 使用 String.format 的小数点后位数

java - 数组列表过滤器

java - 由于错误绑定(bind)扫描 <jar-file> 而创建 entityManagerFactory 时出错

java - Spring 测试 - WebTestClient 无法 Autowiring 。未找到 'WebTestClient' 类型的 beans

java - struts 注释@Result 是否应该只在类级别?