Spring-mvc 5: How to load an externalized properties file using @PropertySource("#{systemProperties[...]}")

330 Views Asked by At

I’m working a rest api and the project structure is a maven multi modules project organized by layer (repository, service and so on) and my project will be hosted on wildfly 13. My working environment is windows 10 (64bits).

I configured my repository module to load an externalized properties file and I got this error:

13:24:04,470 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0183: Service status report WFLYCTL0186: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./RestApiName: java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.company.be.restapiname.config.ServiceContextConfig]; nested exception is java.io.FileNotFoundException: (The system cannot find the path specified)

I suppose instead of get a correct path, a space is returned because there are several spaces between FileNotFoundException: and (The system...).

This is my spring configuration class:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.company.be.restapiname.repository"})
@PropertySource("file:#{systemProperties['CONF']}/persistence.properties")
public class PersistenceContextConfig {
...
}

I made some research and I don’t understand what is wrong because:

It is casual to use SpEL with @PropertySource; systemProperties is a predefined variable accessible with SpEL

The “File:” prefix means spring will use a FileSystemResource to resolve the path

I added the CONF variable via the windows env variable panel and I’m not sure systemProperties can access to the windows environment. So I added the variable as jvm parameter into the startup script of wildfly

set "CONF=c:\dev\wildfly-13.0.0.Final\standalone\config"

set "JAVA_OPTS=-DCONF=%CONF% %JAVA_OPTS%"

And If I replace the Spring expression by the absolute path, I get this message:

WFLYCTL0186: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./restApiName: java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.company.be.restapiname.config.ServiceContextConfig]; nested exception is java.io.FileNotFoundException: c:\dev\wildfly-13.0.0.Final\standalone\config\persistence.properties} (The system cannot find the file specified)

Thanks in advance guys.

0

There are 0 best solutions below