Spring MVC 中使用properties文件

首先要搭建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

 

posted @ 2018-06-30 23:52  喵喵扑  阅读(2410)  评论(0编辑  收藏  举报