Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@PropertySource in a Jar for an external file on the classpath

I'm trying to use the Spring framework's @PropertySource annotation in a Jar to load a properties file from outside the jar, but it's not finding the file.

I need the properties file to be external to the Jar so it can be edited. I don't know the exact location where the file will be, I figured I could just have it anywhere on the classpath.

I'm using the following annotation on my Config class.

@PropertySource('classpath:stc.properties')

And placed stc.properties in the same directory as the created Jar file. I tried specifying the classpath explicitly in the java command, but it still cannot find the file:

java -cp . -jar stc.jar
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.example.stc.Config; nested exception is java.io.FileNotFoundException: class path resource [stc.properties] cannot be opened because it does not exist
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:162)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:299)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
[...]

Etc.

I've also tried using ./ as the classpath, and tried specifying the classpath (with both variants) in the Class-Path attribute of the jar's manifest, but it always gives the same results.

like image 500
brianmearns Avatar asked May 06 '15 14:05

brianmearns


People also ask

What is @PropertySource in Spring boot?

Spring @PropertySource annotation is used to provide properties file to Spring Environment. This annotation is used with @Configuration classes. Spring PropertySource annotation is repeatable, means you can have multiple PropertySource on a Configuration class.

How read properties file from external folder in Spring boot?

Spring boot can read multiple external properties files via command line arguments that can be either a file path or a class path. The numerous external properties files might be found in a single directory or in multiple locations.

How do I configure externalize in Spring boot?

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.


1 Answers

Assuming you have two files, one for local one for production

@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource(value = "${ws.properties}", ignoreResourceNotFound = true)

})

And in tomcat or your jar file , pass on this parameter

-Dws.properties=file:/path-to.properties

I added this in setenv.sh

APPLICATION_OPTS="-Dlog4j.configurationFile=file:$PATH/log4j2.xml -Dlog4j.debug=true -Dapplication.properties=file:$PATH/application.properties

This is possible with Spring 4 only

like image 111
vsingh Avatar answered Nov 06 '22 02:11

vsingh