【发布时间】:2018-09-02 15:27:19
【问题描述】:
目前我的src/main/resources 文件夹中有一些属性文件:
- application.properties
- application-dev.properties
- application-test.properties
现在,当我指定配置文件时,它会同时加载该配置文件的特定文件和通用 application.properties,并用前者覆盖所有内容。
但是,当我的应用程序部署在生产中时,没有传递任何配置文件,因此application.properties 必须是我的生产文件。
这很好,因为我可以覆盖配置文件特定的所有内容。但是,有一个问题;在生产中,我现在需要设置:
spring.datasource.jndi-name=jdbc/appname
当我将它添加到application.properties 时,每个配置文件也会继承它,然后,当我运行开发服务器或测试时,它会给我这个错误:
原因:javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中: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 或 undefined 的方法(因为空不能解决问题)
- 一种将通常继承的属性文件更改为其他内容的方法(我尝试了@PropertySource,但它只添加了更多选项,最终总是回退到 application.properties)
- 通过属性文件完全禁用 JNDI,尽管有以前的属性(我按照 here 尝试了
spring.jndi.ignore=true,但无济于事)
但是要么我不知道怎么做,要么它们不起作用。
【问题讨论】:
-
为什么不在生产环境中使用配置文件
prod? -
我无法更改生产环境,并且在运行应用程序时没有指定任何配置文件。
-
如何运行您的 Spring Boot 应用程序?作为程序还是作为 Servlet? IE。你如何指定一个配置文件?有很多方法可以做到这一点。
-
当我自己运行它时,我使用 gradle 并且可以传递
-Pprofile=something,但是对于部署,我只是向负责部署的公司提供战争,并且无法改变这种行为。 -
如果有人来这里是为了最初问题的实际答案(将
null值设置为属性文件条目),请尝试#{null}- 只要有效你有spring-expressions依赖,我相信
标签: java spring spring-boot properties-file