在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 4517: [Sdoi2016]排列计数 [容斥原理]

    4517: [Sdoi2016]排列计数 题意:多组询问,n的全排列中恰好m个不是错排的有多少个 容斥原理强行推♂倒她 $恰好m个不是错排 $ \[ =\ \ge m个不是错排 - \ge m+1个不 ...

  2. 使用原生JS定位网页元素

    约定:1.<meta name="viewport" content="width=device-width">这种标记简称"wdw&qu ...

  3. LeetCode - 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  4. [HNOI2008] GT考试

    [HNOI2008] GT考试 标签 : DP 矩阵乘法 题目链接 题意 n位数中不出现一个子串的方案数. 题解 \(设dp[i][j]\)为前i位匹配到j时的合法方案数.(所谓合法,就是不能有别的匹 ...

  5. Factorial数列的几种实现方式

    斐波那契数列很常见,实现的方法主要有递归,for,栈等,下面给出代码 import java.math.BigInteger; import java.util.Scanner; import jav ...

  6. Java经典编程题50道之三十一

    将一个数组逆序输出. public class Example31 {    public static void main(String[] args) {        int[] a = { 9 ...

  7. C/C++语言简介之语法结构

    一.顺序结构    顺序结构的程序设计是最简单的,只要按照解决问题的顺序写出相应的语句就行,它的执行顺序是自上而下,依次执行.    例如:a = 3,b = 5,现交换a,b的值,这个问题就好像交换 ...

  8. vim+makefile入门编辑,编译,差错实例

    vim+makefile入门编辑,编译,差错实例 vim makefile 编译 编写代码,一般在vim中编辑完后,输入:wq,在命令行下输入g++ hello.cc -o hello ,出现问题,打 ...

  9. Qt 如何使用 lambda 表达式连接信号和槽?

    connect(camera, static_cast<void(QCamera::*)(QCamera::LockStatus, QCamera::LockChangeReason)>( ...

  10. 《android开发艺术探索》读书笔记(十三)--综合技术

    接上篇<android开发艺术探索>读书笔记(十二)--Bitmap的加载和Cache No1: 使用CrashHandler来获取应用的crash信息 No2: 在Android中单个d ...