1.新建一个spring-config.xml文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!--
<value>file:C://important1.properties</value>
-->
<value>classpath:important1.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"></property>
</bean>
2.新建一个spring-config-service.xml文件
<bean id="bean1" class="com.xxx.ATest">
<property name="iconList" value="${com.xxx.iconList}"></property>
</bean>
3.新建一个iconList.properties文件
com.xxx.iconList=http://image1.jpg\
http://image2.jpg\
http://image3.jpg\
上面要使用\ 否则注入到java类中的数据只会是http://image1.jpg\
4.新建一个java文件
public class ATest{
private String iconList;
public static void main(){
System.out.println(this.iconList);
}
public void setIconList(String iconList){
this.iconList = iconList;
}
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
6.当web项目启动时,iconList.propertis文件中键com.xxx.iconList对应的值就会通过bean1 setter注入到ATest的私有成员iconList,保存到内存中