1. 程式人生 > >Spring MVC 中使用properties文件

Spring MVC 中使用properties文件

hostname smt ssp text 路徑 GC encoding vat work

首先要搭建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[email protected]

Spring MVC 中使用properties文件