property placeholder - @Value in my spring controller does not work -
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 following exception:
caused by: java.lang.illegalargumentexception: not resolve placeholder 'myprop' in string value "${myprop}"
note: properties file myapps-local.properties
in classpath
, contains myprop=delightful
any great....
in xml based configuration need 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 can use values 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 want use values properties files inside @value
annotation bean's fields, need add @confuguration
, @propertysource
annotation bean class. this
@configuration @propertysource("classpath:database.properties") public class appconfig { @value("${jdbc.url}") private string url; @bean public static propertysourcesplaceholderconfigurer propertyconfigindev() { return new propertysourcesplaceholderconfigurer(); } }