Reputation: 125
My controller has
@Value("${myProp}")
private String myProp;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@RequestMapping(value = "myProp", method = RequestMethod.GET)
public @ResponseBody String getMyProp(){
return "The prop is:" + myProp;
}
my applicationcontext.xml
has
<bean id="appConfigProperties" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:MyApps-local.properties" />
</bean>
I get the following exception:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in string value "${myProp}"
Note: My properties file MyApps-local.properties
is in the classpath
and contains
myProp=delightful
any help would be great....
Upvotes: 2
Views: 2734
Reputation: 400
See if this helps
<util:properties id="appConfigProperties" location="location to your properties file" /> <context:property-placeholder properties-ref="appConfigProperties" />
Comes from schema http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
Then in you code where you want your value from property file use it as
@Value("${myPropl}")
private String str;
works for me, let me know if stuck any where :)
Upvotes: 1
Reputation: 5753
In your configuration you have two PropertySourcesPlaceholderConfigurer. The first one define in the DispatcherServlet using Java @Configuration uses the standard environment to find properties. This means properties from system environment and environment variables as well as an defined @PropertySource.
This is not aware of your MyApps-local.properties file specified in the applicationContext.xml. The second PropertySourcesPlaceholderConfigurer in the xml file is aware of the MyApps-local.properties file but it only post process placeholder in the root application context
bean factory post processors are scoped per application context.
Change you web application context to specify the property source.This will add the properties in the file to your environment
@Configuration
@PropertySource("classpath:MyApps-local.properties")
public class WebConfig{
@Autowired
private Environment env;
@RequestMapping(value = "myProp", method = RequestMethod.GET)
public @ResponseBody String getMyProp(){
return "The prop is:" + env.getProperty("myProp");
}
}
In this case you dont need the PropertySourcesPlacheholder as you can query for he properties from the environment. Then keep your applicationContext.xml as is
It will also work with the PropertySourcesPlaceholder @Bean as it also picks properties from the enviroment. However the abover is clean than below
@Configuration
@PropertySource("classpath:MyApps-local.properties")
public class WebConfig{
@Value("${myProp}")
private String myProp;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@RequestMapping(value = "myProp", method = RequestMethod.GET)
public @ResponseBody String getMyProp(){
return "The prop is:" + myProp;
}
}
Upvotes: 1
Reputation: 416
In XML based configuration you need to use PropertyPlaceholderConfigurer
bean
<beans xmlns="http://www.springframework.org/schema/beans" . . . >
. . .
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties" />
</bean>
. . .
</beans>
but you can use values from database.properties
in xml configuration only
<beans xmlns="http://www.springframework.org/schema/beans" . . >
. . .
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
. . .
</beans>
If you want to use values from properties files inside @Value
annotation for bean's fields, you need to add @Confuguration
and @PropertySource
annotation to bean class. Like this
@Configuration
@PropertySource("classpath:database.properties")
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Upvotes: 2
Reputation: 2405
Read carefully this http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
For dev-mode use profile-specific properties
Upvotes: -1