在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. 济南清北学堂游记 Day 3.

    为什么你们都说下午题简单?我反而觉得下午题难...因为上午暴力很好写啊... 第一题惊了是一道博弈论,冷静推理一大波推出了正解,就变成了结论题. 可是结论题不都是水题吗? T2是一道关于异或的题,照例 ...

  2. 2048小游戏代码解析 C语言版

    2048小游戏,也算是风靡一时的益智游戏.其背后实现的逻辑比较简单,代码量不算多,而且趣味性强,适合作为有语言基础的童鞋来加强编程训练.本篇分析2048小游戏的C语言实现代码. 前言 游戏截图:  游 ...

  3. 本地Linux服务器上配置Git

    当我们需要拉取远程服务器代码到本地服务器时,我们首先要确定已经配置了正确的Git账号,可以从~/.gitconfig文件(为隐藏文件,需要使用ls -a查看),以及~/.ssh下的id_rsa.pub ...

  4. centos7安装部署gitlab服务器

    [gitlab需要内存至少4GB]   我这里使用的是centos 7 64bit,我试过centos 6也是可以的! 1. 安装依赖软件 yum -y install policycoreutils ...

  5. 集合的综合练习:Poker牌

    /* 刘意教程示例:*/ package cn.onecool.cot; import java.util.ArrayList; import java.util.Collections; impor ...

  6. nginx的负载均衡集群测试

    分别在3台机子安装nginx和启动nginx服务. dir: 192.168.0.7 另外2台服务器为 192.168.0.5 ,192.168.0.6 在dir  192.168.0.7 上增加配置 ...

  7. Linux下的压力测试工具:ab、http_load、webbench、siege

    一.ab 1.1 介绍 ab是apache自带的一款功能强大的测试工具.      安装了apache一般就自带了. 1.2 下载 同apache. 1.3 安装 同apache. 1.4 安装结果 ...

  8. OpenCV角点检测goodFeaturesToTrack()源代码分析

    上面一篇博客分析了HARRIS和ShiTomasi角点检测的源代码.而为了提取更准确的角点,OpenCV中提供了goodFeaturesToTrack()这个API函数,来获取更加准确的角点位置.这篇 ...

  9. kindeditor编辑器修改文本后保存时发现获取到的内容还是修改前的文本内容

    定义kindeditor的时候要加上一下几个属性设置: KindEditor.ready(function(K) { var editor = K.create("textarea[name ...

  10. 【Unity3D技术文档翻译】第1.4篇 AssetBundle 依赖关系

    上一章:[Unity3D技术文档翻译]第1.3篇 创建 AssetBundles 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Develo ...