简体   繁体   中英

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. 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. 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? 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. 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.

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.

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. By doing this, Spring automagically finds the property in the system props and replaces it. 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).

By putting the properties files outside the war / ear, they can be changed at will, and only the context needs to be restarted. 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 .

for picking env specific values you have couple of options

  • 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.
  • 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

On security question , one the approach is to store encrypted password in prop file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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