spring @value標籤從properties文件中注入屬性值

[java] view plaincopy
  1. @Controller  
  2. @RequestMapping("/value")  
  3. public class ValuePropertyController extends ApplicationController{  
  4.       
  5.     @Value("#{configProperties['jdbc.jdbcUrl']}")  
  6.     private String jdbcUrl;   
  7.       
  8.     @RequestMapping  
  9.     public String value(){  
  10.         System.out.println(jdbcUrl);  
  11.         return "";  
  12.     }  
  13. }  


applicationContext.xml

[html] view plaincopy
  1. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  2.        <property name="locations">  
  3.            <list>  
  4.                <value>classpath:database.properties</value>  
  5.            </list>  
  6.        </property>  
  7.     </bean>  
  8.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
  9.         <property name="properties" ref="configProperties" />  
  10.     </bean>  

database.properties

[html] view plaincopy
  1. jdbc.jdbcUrl=jdbc:mysql://l
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章