How to modify a value in a Java property file

I have a config Properties file, which contains configurable properties, such as database connection details in webapp deployed on Tomcat for example

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 use the spring environment object or @ value from config Property My question is, when you run on dev, how do you make spring's environment objects choose local properties when they run on local and dev properties? In addition, saving sensitive data doesn't sound right. For example, the details in the production database connection properties file will float in the code base So how to add production details in the production environment? Ideally, I want to change them when I like without having to redeploy the application Am I moving in the right direction?

Note - all the answers I see on so discuss changing these properties in Java code I don't want to do this. I want to be able to configure these values independently of the application code

thank you

Solution

You can view spring profiles to load specific files for a specific environment

Alternatively, you can parameterize a file that loads properties from the application context using JNDI properties or environment properties set in the container

Example:

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

When you start it, you can use - DENV config. File = set env at the container level (such as Tomcat) config. file. By doing so, spring automatically finds the property in the system and replaces it If it is not explicitly set (for example, other containers may be used in dev, such as jetty), it will use the given default value (dev.properties in this example)

By placing the properties files outside of war / ear, you can change them at will and only need to restart the context Alternatively, you can view reloadable property placeholders This also helps if you don't want to explicitly store passwords in war

To encrypt the information in the properties file, if you are using spring 3, you can also check: http://www.jasypt.org/spring3.html.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>