Thursday, October 11, 2012

Spring – load property value using @Value


Spring – load multiple property based on utils properties code

pure utils:property, one location
With the utils namespace in Spring 3.0 you can load a property file and use the values in Spring EL expressions.
– Spring config File –
 <util:properties id="someProps" location="classpath:mybasic.properties" />  
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
     ...  
   <property name="url" value="jdbc:h2:tcp://localhost/~/DB/#{someProps['database.dbname']}" />  
   <property name="username" value="#{someProps['database.user']}" />  
   <property name="password" value="#{someProps['database.password']}" />  
     ...  
 </bean>  
To use the property values in Java classes use the the EL expression with the @Value annotation.
– Java code –
 ...  
 HINT:the name you use for someProps can not includes '-'(e.g. can not be  
 words like some-props)  
 String @Value("#{someProps['my.tmp.dir']}") tmpDir;  
 ...  
Multiple property files
Please visit author's page http://develop.nydi.ch/2010/12/spring-multiple-properties/

No comments:

Post a Comment