Spring容器管理各种文件

1. 导入文件

  <import resource="applicationContext-dataSource.xml" />

2. 引用资源配置文件

<context:property-placeholder location="classpath:jdbc.properties,classpath:xxx.properties"/>
或者
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>

  多次引用没有ignore-unresolvable="true"一定会出"Could not resolve placeholder"异常。

  Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,所以就不能使用上面的那种方法去配置,

  可以改如下的格式:

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>

  使用引用的文件内容:

<bean name="userInfo" class="test.UserInfo">  
  <property name="username" value="${db.username}"/>  
  <property name="password" value="${db.password}"/>  
</bean> 

3. 在项目java代码中引用资源文件

  例如引用xxx.properties中的内容

  首先在spring容器中配置:

<util:properties id="settings" location="/WEB-INF/xxx.properties"></util:properties>

  java代码中使用方法:

@Value("#{settings}")   
private Properties file;
或者
@Value("#{settings['test.abc']}")   
private String url;
原文地址:https://www.cnblogs.com/cxyj/p/3885319.html