首页 > 编程语言 > 详细

Spring MVC 中使用properties文件

时间:2018-07-01 00:04:49      阅读:170      评论:0      收藏:0      [点我收藏+]

首先要搭建Spring mvc的环境,然后开始properties文件里的配置:

第一步:在springcontext中注入properties,具体路径自己调整

<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
     <property name="locations">
         <list>
               <value>classpath:config.properties</value>
         </list>
     </property>
     <property name="fileEncoding" value="utf-8" />
</bean>


第二步:在使用的地方加上Value 注解

@Value("#{config[‘email.hostName‘]}")
private String hostName;

@Value("#{config[‘email.address‘]}")
private String address;

 

第三步:使用属性

System.out.println(hostName);
System.out.println(address);

 

附 config.properties:

##配置
email.hostName=smtp.aliyun.com
email.address=lixingwu@aliyun.com

 

Spring MVC 中使用properties文件

原文:https://www.cnblogs.com/lixingwu/p/9249104.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!