通过Spring配置获取properties文件内容实例

第一种方法: 

properties文件内容
dataSource.url="123"
dataSource.username="123"
dataSource.password="123"
dataSource.driverClassName="123"

在配置文件applicationContext.xml配置,如果是单个个properties文件<value>/config/configInfo.properties</value>


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

在类中以成员变量的形式获取properties对应的值

@Value("${dataSource.url}")
private String url;

第二钟方法:

在xml文件获取对应属性,

 <context:property-placeholder location="classpath:conn.properties"/>

context:property-placeholder改配置也是创建一个PropertyPlaceholderConfigurer的bean实例

默认spring只维护一个PropertyPlaceholderConfigurer的bean实例,加如下属性ignore-unresolvable="true"即可有多个

第一种方法不加<property name="ignoreUnresolvablePlaceholders" value="true" />也会与<context:property-placeholder?>冲突

<bean id="mtpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">       
   <property name="driverClassName" value="${dataSource.driverClassName}" />
   <property name="url">
      <value>${dataSource.url}</value>
   </property>
   <property name="username">
      <value>${dataSource.username}</value>
   </property>
   <property name="password">
      <value>${dataSource.password}</value>
   </property>
       <property name="initialSize" value="20" />
   <property name="maxActive" value="500" />
   <property name="maxIdle" value="20" />
   <property name="maxWait" value="500" />
   <property name="defaultAutoCommit" value="true" />
   <!-- <property name="removeAbandoned" value="true" />
   <property name="removeAbandonedTimeout" value="60" /> -->
   <property name="testOnBorrow" value="true"/>  
   <property name="validationQuery" value="select 1 from dual"/>  
</bean>

第一种和第二种配置和获取可以混用

第三种方法:

第一种方法在类中每用一次要声明一个局部变量,也可以通过重写processProperties()的方法获取

public class MyPropertyConfig extends PropertyPlaceholderConfigurer {
   private Map<String, String> ctxPropertiesMap;

   @Override
   protected void processProperties(ConfigurableListableBeanFactory factory, Properties props) throws BeansException {
      super.processProperties(factory, props);
      
      ctxPropertiesMap = new HashMap<String, String>();
      for (Object key : props.keySet()) {
         String keyStr = key.toString();
         String value = props.getProperty(keyStr);
         ctxPropertiesMap.put(keyStr, value);
      }
   }

   public String getVal(String name) {
      return ctxPropertiesMap.get(name);
   }
}

xml配置文件中

<bean id="myPropertyConfig"
     class="xxx.MyPropertyConfig">   <property name="ignoreUnresolvablePlaceholders" value="true" />
   <property name="locations">
      <list>
         <value>classpath:/config/config.properties</value>
         <value>classpath:/config/db.properties</value>
      </list>
   </property>
</bean>

在类中如此写即可调用对应属性

@Resource
private MyPropertyConfig mtpPropertyConfig;
@Test
public void test(){
   myPropertyConfig.getVal("dataSource.url")
}

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值