在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcached server的地址端口信息等,这些配置信息最好独立于jar包或者war包,这样便于修改配置。Spring提供了PropertyPlaceholderConfigurer类来处理这件事情。

一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
Properties代码  
database.url=jdbc:mysql://localhost/smaple  
database.driver=com.mysql.jdbc.Driver  
database.user=root  
database.password=123  
 
2.消息服务配置文件demo-mq.properties:
Properties代码  
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory  
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000  
mq.java.naming.security.principal= jjktyjydjd 
mq.java.naming.security.credentials= jytjtyjjt
jms.MailNotifyQueue.consumer=5  
 
3.远程调用的配置文件demo-remote.properties:
Properties代码  
remote.ip=localhost  
remote.port=16800  
remote.serviceName=test  
 
一、系统中需要加载多个Properties配置文件

应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
Xml代码  
 
  (1)<!-- 将多个配置文件读取到容器中,交给Spring管理 -->  
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
      <property name="locations">  
         <list>  
            <!-- 这里支持多种寻址方式:classpath和file -->  
            <value>classpath:/opt/demo/config/demo-db.properties</value>  
            <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
            <value>file:/opt/demo/config/demo-mq.properties</value>  
            <value>file:/opt/demo/config/demo-remote.properties</value>  
         </list>  
      </property>  
   </bean>   
 
   <!-- 使用MQ中的配置 -->  
   <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">  
     <property name="environment">  
       <props>  
         <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>  
         <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>  
         <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>  
         <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>  
         <prop key="userName">${mq.java.naming.security.principal}</prop>  
         <prop key="password">${mq.java.naming.security.credentials}</prop>  
       </props>  
      </property>  
   </bean>

(2)

 

<!-- 将多个配置文件读取到容器中(配置文件放在了tomcat中的),交给Spring管理 -->

      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${catalina.home}/etc/domain/domain.properties</value>
                <value>file:${catalina.home}/etc/dcas/dcas-client.properties</value>
<value>file:${catalina.home}/etc/fruadmetrix/fruadmetrix.properties</value>
            </list>
        </property>
    </bean>

<!--使用配置的另一种写法>

 
   <bean id="freightConfig" class="com.olymtech.dzg.freight.enquiry.rest.config.FreightConfig">
     <property name="fraudmetrixUrl" value="${fraudmetrix.fraudmetrixUrl}"></property>
     <property name="partner_code" value="${fraudmetrix.partner_code}"></property>
     <property name="secret_key" value="${fraudmetrix.secret_key}"></property>
     <property name="event_id" value="${fraudmetrix.event_id}"></property>
      </bean>

(3)也可以将list抽取出来

<!-- 将多个配置文件位置放到列表中 -->

 
    <bean id="propertyResources" class="java.util.ArrayList">  
     <constructor-arg>  
       <list>  
        <!-- 这里支持多种寻址方式:classpath和file -->  
        <value>classpath:/opt/demo/config/demo-db.properties</value>  
        <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
        <value>file:/opt/demo/config/demo-mq.properties</value>  
        <value>file:/opt/demo/config/demo-remote.properties</value>  
       </list>  
     </constructor-arg>  
    </bean>   
 
    <!-- 将配置文件读取到容器中,交给Spring管理 -->  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations" ref="propertyResources" />  
    </bean>  

spring+jidi读取property的配置文件的更多相关文章

  1. 通过spring工厂读取property配置文件

    /** * Created by ywq on 2016/6/30. */ @Named public class PropertyConfig { private static AbstractBe ...

  2. Spring利用propertyConfigurer类 读取.property数据库配置文件

    (1).基本的使用方法是: <bean id="propertyConfigurerForAnalysis" class="org.springframework. ...

  3. Spring Boot 读取外部的配置文件

    Spring Boot 程序会按优先级从下面这些路径来加载application.properties 或者 application.yml 配置文件 jar包同级目录下的/config目录jar包同 ...

  4. spring boot读取配置文件

    一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文     ...

  5. (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制

    前面讲过,spring的生命周期为:实例化前奏-->实例化-->实例化后期-->初始化前期-->初始化-->初始化后期-->bean的具体调用-->销毁前-- ...

  6. maven工程读取resource下配置文件

    maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources   下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 ...

  7. Spring Boot源码分析-配置文件加载原理

    在Spring Boot源码分析-启动过程中我们进行了启动源码的分析,大致了解了整个Spring Boot的启动过程,具体细节这里不再赘述,感兴趣的同学可以自行阅读.今天让我们继续阅读源码,了解配置文 ...

  8. 搭建spring+mybatis+struts2环境的配置文件

    1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=& ...

  9. spring+hibernate+jpa+Druid的配置文件,spring整合Druid

    spring+hibernate+jpa+Druid的配置文件 spring+hibernate+jpa+Druid的完整配置 spring+hibernate+jpa+Druid的数据源配置 spr ...

随机推荐

  1. BZOJ 4216: Pig [分块]

    传送门 询问区间和...内存3M 分块前缀和... 我连swap都是这么l=l^r , r=l^r , l=l^r写的.... #include <cstdio> typedef long ...

  2. 闭包和es6实现循环绑定li输出固定索引值

    首先我们需要一个html结构 <div > <ul> <li>a</li> <li>a</li> <li>a< ...

  3. CSS小技巧使用

    1.清除浮动 浮动给我们的代码带来的麻烦,想必不需要多说,我们会用很多方式来避免这种麻烦,其中我觉得最方便也是兼容性最好的一种是....// 清除浮动 .clearfix{ zoom: 1; } .c ...

  4. php+redis 学习 二 悲观锁

    <?php header('content-type:text/html;chaeset=utf-8'); /** * redis实战 * * 实现悲观锁机制 * */ $timeout = 5 ...

  5. LeetCode - 185. Department Top Three Salaries

    The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...

  6. MySQL5.7 group by新特性,报错1055

    项目中本来使用的是mysql5.6进行开发,切换到5.7之后,突然发现原来的一些sql运行都报错,错误编码1055,错误信息和sql_mode中的"only_full_group_by&qu ...

  7. Filezilla Server 出现Error, could not connect to server解决办法

    打开任务管理器:Win+R:services.msc找到Filezilla Server并启动服务

  8. 【转】egametang框架简介

    讨论QQ群 : 474643097 1.可用VS单步调试的分布式服务端,N变1 一般来说,分布式服务端要启动很多进程,一旦进程多了,单步调试就变得非常困难,导致服务端开发基本上靠打log来查找问题.平 ...

  9. Python 脚本实现对 Linux 服务器的监控

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 原文地址 由于原文来自微信公众号,并且脚本都是图片,所以这里 ...

  10. 一位IT男的7年工作经验总结

    一位IT男的7年工作经验总结 1.分享第一条经验:"学历代表过去.能力代表现在.学习力代表未来." 其实这是一个来自国外教育领域的一个研究结果.相信工作过几年.十几年的朋友对这个道 ...