Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring application context external properties?

i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- <property name="locations" value="classpath:/springcontext.properties"/>  --> <property name="locations" value ="config/springcontext.properties" /> 

The outcommented one is working and the other one i dont get to work :/ Can someone help?

Edit: Thx 4 comments so far.

Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?

like image 576
Dennis Ich Avatar asked Nov 25 '11 14:11

Dennis Ich


People also ask

How do you call external properties from spring boot?

properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring. config. location environment property (comma-separated list of directory locations, or file paths).

Where does Spring look for application properties?

Spring Boot Framework comes with a built-in mechanism for application configuration using a file called application. properties. It is located inside the src/main/resources folder, as shown in the following figure.

How do you externalize a constants from a Spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.


2 Answers

You can try something like this:

<context:property-placeholder          location="${ext.properties.dir:classpath:}/servlet.properties" /> 

And define ext.properties.dir property in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:

-Dext.properties.dir=file:/usr/local/etc/ 

BTW, very useful blog post.

like image 174
masted Avatar answered Oct 02 '22 14:10

masted


<context:property-placeholder location="classpath*:spring/*.properties" /> 

If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above

<property name="locations" value ="config/springcontext.properties" /> 

this will be pointing to web-inf/classes/config/springcontext.properties

like image 42
fmucar Avatar answered Oct 02 '22 14:10

fmucar