用 @Value("${xxxx}")注解从配置文件读取值的用法

来源:互联网 发布:美国各州人口密度数据 编辑:程序博客网 时间:2024/06/03 17:04

1.  用法:

从配置properties文件中读取init.password 的值。

[java] view plain copy print?
  1. @Value("${init.password}")  
  2.  private String initPwd;    


2. 在spring的配置文件中加载配置文件dbconfig.properties :

[html] view plain copy print?
  1.  <!-- 加载配置文件 -->  
  2.    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.     <property name="fileEncoding" value="UTF-8"/>  
  4.     <property name="locations">  
  5.         <list>  
  6.             <value>classpath:dbconfig.properties</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  
或者这样加载:
[html] view plain copy print?
  1. <context:property-placeholder location="classpath:dbconfig.properties" />  

或者这样加载:

[html] view plain copy print?
  1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
  2.     <property name="location">  
  3.     <value>dbconfig.properties</value>  
  4.     </property>  
  5. </bean>  



3. dbconfig.properties  文件:
[html] view plain copy print?
  1. #MD5  
  2. password.algorithmName=md5  
  3. password.hashIterations=2  
  4. #initpwd  
  5. init.password=admin  

阅读全文
0 0
原创粉丝点击