Java 将应用程序属性重写为未定义/未设置

Java 将应用程序属性重写为未定义/未设置,java,spring-boot,Java,Spring Boot,我不确定这是否是一个有效的问题,但我想知道这是否可能 Spring启动项目有一个application.properties和几个特定于概要文件的属性。特定于配置文件的属性将使用application-profile.properties中定义的任何内容覆盖已定义的application.properties,并且还会添加仅属于特定于配置文件的属性的那些属性。下图: 应用程序属性 key1=value1 key2=value2 key1=valueProfile1 key3=valueProf

我不确定这是否是一个有效的问题,但我想知道这是否可能

Spring启动项目有一个application.properties和几个特定于概要文件的属性。特定于配置文件的属性将使用application-profile.properties中定义的任何内容覆盖已定义的application.properties,并且还会添加仅属于特定于配置文件的属性的那些属性。下图:

应用程序属性

key1=value1
key2=value2
key1=valueProfile1
key3=valueProfile3
keySpecial=specialValue
key1=value1
key2=value2
key1=valueSpecial1
//unset or undefine keySpecial
keyAlternateSpecial=specialAlternateValue
key3=valueSpecial3
application-profile.properties

key1=value1
key2=value2
key1=valueProfile1
key3=valueProfile3
keySpecial=specialValue
key1=value1
key2=value2
key1=valueSpecial1
//unset or undefine keySpecial
keyAlternateSpecial=specialAlternateValue
key3=valueSpecial3
当应用程序使用此配置文件启动时,它看到的最终属性如下所示:

key1=valueProfile1
key2=value2
key3=valueProfile3
keyAlternateSpecial=specialAlternateValue
key1=valueSpecial1
key2=value2
key3=valueSpecial3
简而言之,您拥有公共属性和概要属性的并集,概要属性值附加并覆盖公共属性

但是,如果在天知道是什么场景中,我需要在公共应用程序中定义一个属性。properties但是当应用程序在一个特定概要文件中启动时,我需要它是“未定义”的。下图:

应用程序属性

key1=value1
key2=value2
key1=valueProfile1
key3=valueProfile3
keySpecial=specialValue
key1=value1
key2=value2
key1=valueSpecial1
//unset or undefine keySpecial
keyAlternateSpecial=specialAlternateValue
key3=valueSpecial3
应用-特殊属性

key1=value1
key2=value2
key1=valueProfile1
key3=valueProfile3
keySpecial=specialValue
key1=value1
key2=value2
key1=valueSpecial1
//unset or undefine keySpecial
keyAlternateSpecial=specialAlternateValue
key3=valueSpecial3
现在,当应用程序以这个“特殊”配置文件启动时,我希望它看到以下最终属性:

key1=valueProfile1
key2=value2
key3=valueProfile3
keyAlternateSpecial=specialAlternateValue
key1=valueSpecial1
key2=value2
key3=valueSpecial3
请注意,当应用程序在此特殊配置文件中运行时,keySpecial未定义,甚至不存在

这可能吗

注意:我知道我可以避免在common application.properties中定义“keySpecial”,而在all其他特定于配置文件的属性中定义它们。并仅在“特殊”配置文件属性中指定“KeyAlternational”

更多信息:

让我感到疑惑的场景是spring boot数据源jndi属性

仅此属性的存在就使得应用程序忽略其他数据源属性(类、url、用户名、密码),即使它们已设置


我不允许从“application.properties”中删除jndi属性。但是我想取消设置/取消定义它,并将其他数据源属性(类、url、用户名、密码)添加到“特殊”配置文件属性中。

在我的用例中,我通过在默认配置文件中定义JNDI属性解决了这个问题,该属性在没有定义其他配置文件时被激活,通过这种方式,在开发过程中,我可以在没有JNDI的情况下使用不同的数据源

这是我的
application.yml
文件的摘录,不知道这是否适合你

spring:
  jpa:
    database: POSTGRESQL
    hibernate:
      ddl-auto: none
# Default profile, active by default on JBoss since no spring profiles are activated
---
spring:
  profiles: default
  datasource:
    jndi-name: java:jboss/datasources/anagraficaDS
# Development configuration
---
spring:
  profiles: development
  datasource:
    platform: postgres
    url: jdbc:postgresql://localhost:5432/my-db
    username: root
    password: secret
    driverClassName: org.postgresql.Driver

通过设置
spring.datasource.jndi name=false
,可以从application.property中假删除spring.datasource.jndi-name。这样
@conditionalnproperty(prefix=“spring.datasource”,name=“jndi name”)
就不会启用自动配置类。有关详细信息,请参阅ConditionalOnProperty javadoc。

我在寻找同样的东西(与jndi相同的用例),您是否找到了解决方案?@Fabio很抱歉回复太晚。我没能找到解决办法。我会在这里张贴,如果我发现任何。如果你发现了什么,请在这里发帖。其他问题也一样。这是一个解决办法。我在问题“注意:我知道我可以避免在common application.properties中定义“keySpecial”,并在所有其他特定于配置文件的属性中定义它们。并且只在“special”配置文件属性中指定“keysalternational”。这正是你(我也是)所做的。我想仍然没有一个合适的方法来取消定义/取消设置应用程序的一个属性源中定义的属性。谢谢你,这是一个很好的技巧,也帮助了我。我认为Spring应该通过寻找一个空值并以同样的方式处理它来更直接地支持这一点。