在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. open-falcon-agent插件使用

    说明 Plugin可以看做是对agent功能的扩充.使用插件可以对采集脚本进行统一管理,方便定制修改,也可以免去在crontab中添加计划任务. 开启plugin功能 # 修改agent配置文件 &q ...

  2. nodejs开启服务器端口

    var http = require('http'); var server = http.createServer(function(req, res) { res.writeHead(200, { ...

  3. centos 6.8 下安装redmine(缺陷跟踪系统)

    一.实验环境 centos6.8 64位 所需安装包: ruby-2.3.4.tar.gz.rubygems-1.8.25.tgz.redmine-2.3.2.tar.gz 二.安装步骤 1.安装必要 ...

  4. Jetty容器

    ♣Jetty和Tomcat的区别 ♣Jetty下载.安装 ♣eclipse安装jetty插件 ♣第一个jetty测试(maven+jetty) 1.Jetty和Tomcat的区别   Jetty 是一 ...

  5. egametang启动配置

    egametang的启动配置文件可以在Unity的Tools->命令行配置中修改保存然后启动 如果需要添加自定义的启动配置项目,只需要修改客户端的 ServerCommandLineEditor ...

  6. jq模仿雨滴下落的动画

    效果如图: 实现思路:定时器每隔x秒生成宽高.下落速度(即动画执行时间).left随机的div. 1.CSS: body{ overflow: hidden;/*这是为了防止出现滚动条*/ } .co ...

  7. 华为云照片的爬虫程序更新(python3.6)

    一.背景: 每年终都有一个习惯,就是整理资料进行归档,结果发现手机照片全备份在华为云里,在官网上找了一圈,没找到官方的pc工具用来同步照片. 于是找出上次写的程序,看看能不能爬到数据,然而……果然不好 ...

  8. js “top、clientTop、scrollTop、offsetTop…”

    当要做一些与位置相关的插件或效果的时候,像top.clientTop.scrollTop.offsetTop.scrollHeight.clientHeight.offsetParent...看到这么 ...

  9. ArrayList源码阅读

    前言 数组是我们最常用最简单的数据结构,Java里对数组做了一个简单的包装,就是ArrayList,提供自动扩容的功能. 最常用法 list在我们日常代码中最为常用的做法是创建一个list,放入数据, ...

  10. Jquery就是这么简单

    什么是Jquery? Jquey就是一款跨主流浏览器的JavaScript库,简化JavaScript对HTML操作 就是封装了JavaScript,能够简化我们写代码的一个JavaScript库 为 ...