Spring Property place holder / multiple applications within the same tomcat/jvm - spring

Is there a way to use Spring 3 property place holder to load application specific properties without conflicting with other applications within the same tomcat/jvm ?

All of your application specific properties are residing inside your jar and tomcat will only make them available to your application.
Just put them under webapps/${app_war}/WEB-INF/classes/app.properties and your spring config will pick them up from classpath.
System properties via -D startup parameters are shared, but not application specific.

Related

Override application properties of spring application

Currently we developing a web application on we use spring framework, this application contains several modules, so we chose to put all the configuration in a module named "shared-module" when we using the IDE we can specify the propority file of profile but when we try to launch this application outside of IDE we can not point to the correct property file.
Application strucuture:
shared-module--(injected)-->application.jar
See Externalized Configuration properties order described here. This documentation have clear description of supported application and profile specific properties. Please provide additional information if it does not address specific scenario you've problem with.

Use External properties file

Am looking to pass variables at run time once war file is deployed on tomcat ..
How can i use application.properties whcih is in classplath along with another properties file ex. abcd.properties located at particular directory..Am basically looking to set additional classpath and read value from properties file in that path along with default classpath location for war deployment.
Am using Spring boot .One of the way is to pass all properties to database end , but am looking for a file based i.e properties based workout.
(Having multiple applications on same tomcat instance.)
Spring Boot App --> run as --> run configurations. Now here in VM arguments add Dproperties.location="Path of the properties".
Now, in your Spring Boot application use the annotation #PropertySource("file:${properties.location}/propertiesfileName.properties") just above the class declaration.
Autowire Environment in your class. use env.getProperty("propertyname").
You can access the values from application.properties as usual using #Value annotation. Hope this helps.

What is best practice management properties for me?

What can I do for best practice management properties?
I want to set different properties each deployed environment.
Some of my developer cannot detect my real server properties.
I want to build project using same command (using maven. not want to -P dev, -P production options)
I don't want to too many source code to load my properties.(like implements db access)
Can be continue my service without restart when some properties has been changed.
My service developed using spring-web.
You have 2 issues:
1) How to have your properties outside of the war file, I would suggest you do the following:
#PropertySource(value={"classpath:/config.properties", "file:${configRoot}/config.properties"}, ignoreResourceNotFound = true)
Then when you start your app you can specify configRoot as a system property to the JVM i.e -DconfigRoot=/var/config. You can then specify a default config which will be pulled from inside the war. Using the ignoreResourceNotFound if the file:${configRoot}/config.properties"} can not be found the first one will be used. i.e. you can have a default inside the war and override it at runtime with the JVM system parameter.
2) How to automatically refresh
Look at this answer to tell spring to refresh it's properties on a schedule:

Spring boot - how to modify config properties from an external application

I have a spring boot application and it has an application.yml file that contains a bunch of properties. I am writing a separate test harness for this application and using that application I would like to modify some properties after the main application starts up. I am thinking that I need to add a new REST endpoint that will implement the PUT method and will change the properties. Using that PUT method I should be able to change any property that is defined in the application.yml file. Is that the correct approach. Is there a sample application that I can take a look at.

No plain text passwords in Spring Boot’s application.properties

Having something like
security.user.password = plainTextPassword
inside Spring Boot’s application.properties is obviously an anti-pattern as it prevents the code from being pushed to a public SCM. In my non-Spring Boot projects I use
security.user.password = ${myPasswordFromMavenSettingsXML}
and put a corresponding <properties/> reference inside my pom.xml.
Using Maven’s resource filter plugin the passwords are replaced at build time so the application have access to actual plain text passwords after it has been build and deployed.
For some reason Maven’s resource filter plugin does not work in this case. Is there a way to not commit plain text passwords to an SCM and let Spring Boot to insert them at build time?
Spring boot has multiple mechanisms to provided externalized configuration. Some examples are command line arguments, environment variables and also application properties outside of your packaged JAR.
What I usually do:
Locally we configured several environment variables. Most (if not all) IDE's allow you to configure environment variables from within the run configuration.
For example if you don't want to expose the spring.datasource.password property you could set an environment variable called SPRING_DATASOURCE_PASSWORD.
When we deploy on another environment, we usually choose to add another application.properties or application.yml file within the same folder as the application JAR/WAR, since Spring boot picks that up as well.
Another solution is to use Spring cloud since it has a config service which can be used as a microservice to provide configuration. The configuration can be versioned using SCM as well, but you can put it on a separate system that is not connected to your source code.

Resources