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
原文地址:https://www.cnblogs.com/lixingwu/p/9249104.html