简体   繁体   English

如何修改java属性文件中的值

[英]How do you modify values in java properties file

I have a config.properties file which contains configurable properties eg database connection details in a webapp deployed on tomcat. 我有一个config.properties文件,其中包含可配置的属性,例如在tomcat上部署的webapp中的数据库连接详细信息。 eg 例如

local.driver: com.mysql.jdbc.Driver
local.username:myuser
local.password:mypass

dev.driver: com.mysql.jdbc.Driver
dev.username:mydevuser
dev.password:mydevpass

I can retrieve the values from config.properties using spring Environment object or @Value. 我可以使用spring Environment对象或@Value从config.properties中检索值。 My question is how do you make Spring's environment object pick local properties when running on local and dev properties when running on dev? 我的问题是当你在dev上运行时,如何让Spring的环境对象在本地和dev属性上运行时选择本地属性? Also it doesn't sound right to save sensitive data eg production database connection details in properties file which will float around in code base. 另外,保存敏感数据听起来也不对,例如属性文件中的生产数据库连接细节将在代码库中浮动。 So how do you add production detail when in production environment? 那么在生产环境中如何添加生产细节呢? Ideally I would want to change them as and when I like and not have to redeploy the app. 理想情况下,我希望在我喜欢的时候更改它们,而不必重新部署应用程序。 Am I going the right direction? 我正朝着正确的方向前进吗?

Note - All the answers I have seen on SO discuss changing these properties within java code. 注 - 我在SO上看到的所有答案都讨论了在java代码中更改这些属性。 I don't want to do that I want to be able to configure these values independent of the application code. 我不想这样做,我希望能够独立于应用程序代码配置这些值。

Thanks 谢谢

You can have a look at spring profiles to load a specific file for a specific environment. 您可以查看spring配置文件以加载特定环境的特定文件。

Alternatively, you can also parameterize the file from where the properties are loaded in the application context using a JNDI property or an environment property set in the container. 或者,您还可以使用JNDI属性或容器中设置的环境属性参数化从应用程序上下文中加载属性的文件。

Example: 例:

<context:property-placeholder ignore-unresolvable="true" location="${env.config.file:classpath*:META-INF/spring/profiles/dev/dev.properties}" />

The env.config.file can be set at the container level (say Tomcat) using -Denv.config.file= when starting it. 启动它时,可以使用-Denv.config.file =在容器级别(比如Tomcat)设置env.config.file。 By doing this, Spring automagically finds the property in the system props and replaces it. 通过这样做,Spring自动在系统中找到属性并替换它。 If you don't set it explicitly (for example, in dev where you might use some other container, such as jetty), it would use the given default value (in this example, dev.properties). 如果未明确设置它(例如,在dev中可能使用其他容器,例如jetty),它将使用给定的默认值(在此示例中为dev.properties)。

By putting the properties files outside the war / ear, they can be changed at will, and only the context needs to be restarted. 通过将属性文件放在war / ear之外,可以随意更改它们,并且只需要重新启动上下文。 Alternatively, you could look into re-loadable property placeholders. 或者,您可以查看可重新加载的属性占位符。 This also helps if you don't want passwords stored in the war in clear. 如果您不希望明确存储在战争中的密码,这也会有所帮助。

For encrypting information in the property files, if you're using Spring 3, you can also check: http://www.jasypt.org/spring3.html . 要加密属性文件中的信息,如果您使用的是Spring 3,还可以查看: http//www.jasypt.org/spring3.html

for picking env specific values you have couple of options 为了获取env特定值,您有几个选项

  • If you can create multiple properties file based on env then you can use Spring profile feature (ie spring.profiles.active), this will allow to control properties file to loaded via JVM parameter. 如果您可以基于env创建多个属性文件,那么您可以使用Spring配置文件功能(即spring.profiles.active),这将允许控制通过JVM参数加载的属性文件。
  • If you still want to keep all the stuff in single fle then you can override PropertyPlaceholderConfigurer to take env details from JVM parameter or default to some value 如果您仍想将所有内容保存在单个文件中,则可以覆盖PropertyPlaceholderConfigurer以从JVM参数获取env详细信息或默认为某个值

On security question , one the approach is to store encrypted password in prop file. 在安全问题上,一种方法是将加密密码存储在prop文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM