Spring 属性文件将 null 设置为 属性
Spring properties file set null to property
目前我的 src/main/resources
文件夹中有几个 属性 文件:
- application.properties
- application-dev.properties
- application-test.properties
现在,当我指定一个配置文件时,它会加载该配置文件的特定文件和一般文件 application.properties
,并用前者覆盖所有内容。
但是,当我的应用程序部署到生产环境时,没有传递任何配置文件,因此 application.properties
必须 是我的生产文件。
这很好,因为我可以覆盖特定配置文件中的所有内容。但是,有一个问题;在生产中,我现在需要设置:
spring.datasource.jndi-name=jdbc/appname
当我将它添加到 application.properties
时,每个配置文件也继承它,然后,当我 运行 开发服务器或测试时,它给我这个错误:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
因为我确实不在我自己的环境中使用这些 JNDI 东西;我使用以下属性:
spring.datasource.url=jdbc:url
spring.datasource.username=user
spring.datasource.password=pass
我的问题是:我一辈子都无法覆盖这个 属性。如果我在 env 特定文件中添加其中任何一个,它将被忽略并且错误仍然存在:
spring.datasource.jndi-name= # empty string, does nothing
spring.datasource.jndi-name=<null> # read somewhere it meant null, doesn't work
spring.datasource.jndi-name=${inexistentProp} # I though it might return null, but gives an error
那么,我能做什么?我想到了几个解决方案:
- 一种在 spring 属性 文件中将 属性 值设置为适当的 null 或未定义的方法(因为空不能解决问题)
- 一种将通常继承的 属性 文件更改为其他文件的方法(我试过@PropertySource,但它只添加了更多选项,最终总是回退到 application.properties)
- 通过属性文件完全禁用 JNDI,尽管以前 属性(我按照 here 尝试
spring.jndi.ignore=true
,但无济于事)
但是要么我不知道怎么做,要么它们不起作用。
Spring 引导文档,24.4 Profile-specific Properties:
In addition to application.properties
files, profile-specific properties can also be defined by using the following naming convention: application-{profile}.properties
. The Environment
has a set of default profiles (by default, [default]
) that are used if no active profiles are set. In other words, if no profiles are explicitly activated, then properties from application-default.properties
are loaded.
The spring.profiles.include
property can be used to unconditionally add active profiles.
因此,使用以下 属性 创建一个 application-default.properties
文件:
spring.profiles.include=prod
现在将 spring.datasource.jndi-name=jdbc/appname
属性 从 application.properties
文件移动到 application-prod.properties
文件。
当您 运行 使用特定配置文件编码时,将不会使用 prod
配置文件,并且 spring.datasource.jndi-name
将是未定义的。
当您在生产中编写 运行 代码时,如果未指定配置文件,默认情况下会包含 prod
配置文件,并且将定义 spring.datasource.jndi-name
。
你当然可以只把生产属性放在 application-default.properties
本身,但是上面的方法让你更清楚你在做什么。
如果您最终拥有多个配置文件,这也会让事情变得更容易。例如。你有 prod
vs dev
vs test
。但是,如果您有独立的配置文件集,例如 foo
与 bar
,并且默认生产环境应该是 prod,foo
怎么办?允许备用生产环境使用 prod,bar
,每个都可以单独测试(test,foo
和 test,bar
)。
通过使用 default
配置文件仅包含其他配置文件,而不定义任何属性,您现在可以根据需要手动混合和匹配配置文件。
目前我的 src/main/resources
文件夹中有几个 属性 文件:
- application.properties
- application-dev.properties
- application-test.properties
现在,当我指定一个配置文件时,它会加载该配置文件的特定文件和一般文件 application.properties
,并用前者覆盖所有内容。
但是,当我的应用程序部署到生产环境时,没有传递任何配置文件,因此 application.properties
必须 是我的生产文件。
这很好,因为我可以覆盖特定配置文件中的所有内容。但是,有一个问题;在生产中,我现在需要设置:
spring.datasource.jndi-name=jdbc/appname
当我将它添加到 application.properties
时,每个配置文件也继承它,然后,当我 运行 开发服务器或测试时,它给我这个错误:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
因为我确实不在我自己的环境中使用这些 JNDI 东西;我使用以下属性:
spring.datasource.url=jdbc:url
spring.datasource.username=user
spring.datasource.password=pass
我的问题是:我一辈子都无法覆盖这个 属性。如果我在 env 特定文件中添加其中任何一个,它将被忽略并且错误仍然存在:
spring.datasource.jndi-name= # empty string, does nothing
spring.datasource.jndi-name=<null> # read somewhere it meant null, doesn't work
spring.datasource.jndi-name=${inexistentProp} # I though it might return null, but gives an error
那么,我能做什么?我想到了几个解决方案:
- 一种在 spring 属性 文件中将 属性 值设置为适当的 null 或未定义的方法(因为空不能解决问题)
- 一种将通常继承的 属性 文件更改为其他文件的方法(我试过@PropertySource,但它只添加了更多选项,最终总是回退到 application.properties)
- 通过属性文件完全禁用 JNDI,尽管以前 属性(我按照 here 尝试
spring.jndi.ignore=true
,但无济于事)
但是要么我不知道怎么做,要么它们不起作用。
Spring 引导文档,24.4 Profile-specific Properties:
In addition to
application.properties
files, profile-specific properties can also be defined by using the following naming convention:application-{profile}.properties
. TheEnvironment
has a set of default profiles (by default,[default]
) that are used if no active profiles are set. In other words, if no profiles are explicitly activated, then properties fromapplication-default.properties
are loaded.
The
spring.profiles.include
property can be used to unconditionally add active profiles.
因此,使用以下 属性 创建一个 application-default.properties
文件:
spring.profiles.include=prod
现在将 spring.datasource.jndi-name=jdbc/appname
属性 从 application.properties
文件移动到 application-prod.properties
文件。
当您 运行 使用特定配置文件编码时,将不会使用 prod
配置文件,并且 spring.datasource.jndi-name
将是未定义的。
当您在生产中编写 运行 代码时,如果未指定配置文件,默认情况下会包含 prod
配置文件,并且将定义 spring.datasource.jndi-name
。
你当然可以只把生产属性放在 application-default.properties
本身,但是上面的方法让你更清楚你在做什么。
如果您最终拥有多个配置文件,这也会让事情变得更容易。例如。你有 prod
vs dev
vs test
。但是,如果您有独立的配置文件集,例如 foo
与 bar
,并且默认生产环境应该是 prod,foo
怎么办?允许备用生产环境使用 prod,bar
,每个都可以单独测试(test,foo
和 test,bar
)。
通过使用 default
配置文件仅包含其他配置文件,而不定义任何属性,您现在可以根据需要手动混合和匹配配置文件。