您的位置:首页 > 产品设计 > UI/UE

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

2017-09-11 20:08 495 查看
1.  用法:

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

[java]
view plain
copy
print?

@Value("${init.password}")  
 private String initPwd;    

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

[html]
view plain
copy
print?

 <!-- 加载配置文件 -->  
   <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
    <property name="fileEncoding" value="UTF-8"/>  
    <property name="locations">  
        <list>  
            <value>classpath:dbconfig.properties</value>  
        </list>  
    </property>  
</bean>  

或者这样加载:

[html]
view plain
copy
print?

<context:property-placeholder location="classpath:dbconfig.properties" />  

或者这样加载:

[html]
view plain
copy
print?

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
    <property name="location">  
    <value>dbconfig.properties</value>  
    </property>  
</bean>  

3. dbconfig.properties  文件:

[b][html]

view plain
copy
print?

#MD5  
password.algorithmName=md5  
password.hashIterations=2  
#initpwd  
init.password=admin  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐