Can't import property value when using placeholder
I am using Spring Boot.
I have declared properties in an external file outside the classpath.
I added this to one of my XML files:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>file:///d:/etc/services/pushExecuterService/pushExecuterServices.properties</value>
</list>
</property>
</bean>
However, I am still getting this error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'configuration.serviceId' in string value "${configuration.serviceId}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
I added a breakpoint in the class PropertiesLoaderSupport
with this method:
public void setLocations(Resource... locations) {
this.locations = locations;
}
I noticed that this method is being called multiple times, and in one of them I noticed that in the space parameters filled with:
URL [file:/d:/etc/services/pushExecuterService/pushExecuterServices.properties]
However, I am still getting this error.
-
I have double checked in my project and I have no additional PropertyPlaceholderConfigurer beans (did not check external dependencies)
-
I ran my app with hardcoded params inside the xml which I see in spring-boot logs:
2015-01-05 18:56:52.902 INFO 7016 --- [ main] o.s.b.f.c.PropertyPlaceholderConfigurer: Loading properties file from URL [file:/d:/etc/services/pushExecuterService/pushExecuterServices.properties]`
So I'm not sure what's going on. any conclusions?
thank.
source to share
Spring Boot supports Java based configuration. To add configuration properties, we can use the @PropertySource annotation along with the @Configuration annotation.
Properties can be stored in any file. Property values ββcan be injected directly into beans using the @Value annotation:
@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfiguration {
@Value("${mail.protocol}")
private String protocol;
@Value("${mail.host}")
private String host;
}
The @PropertySource attribute tag specifies the resource location (s) for the loaded properties file. For example, "classpath: /com/myco/app.properties" or "file: / path / to / file"
But Spring Boot provides an alternative property handling method that allows strongly typed beans to manage and validate your application's configuration: @ConfigurationProperties
See this blog post for an example of using @ConfigurationProperties: http://blog.codeleak.pl/2014/09/using-configurationproperties-in-spring.html
For @PropertySource example, you can check this article: http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html
source to share