zoukankan      html  css  js  c++  java
  • 配置文件的那点事

    1.配置文件

    配置文件在web工程中已经是屡见不鲜了,比如数据源的配置,谁都会想到放到配置文件中。 Spring中有个<context:property-placeholder location=""/>标签,可以用来加载properties配置文件,location是配置文件的路径。看下面的例子:

    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />  
        <property name="jdbcUrl" value="${jdbc.url}" />  
        <property name="user" value="${jdbc.username}" />  
        <property name="password" value="${jdcb.password}" />  
     </bean>

    配置文件:

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/sys?useUnicode=true&characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=123456

    2.多环境

    上面的例子很好的解决了开发中的配置问题,然而在实际工程中还有涉及多环境的情况。开发环境,测试环境,线上环境等。我们希望连配置文件都懒得改,毕竟在发布的时候程序猿和运维还要沟通一次,可能粗心的程序猿忘掉了什么配置要改结果一群人都要加班,这是我不乐见的。

    spring3给我们提供了profile,每个环境一个配置文件,这就不用改来改去了。来看看这个例子:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx    
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
        
        <import resource="spring-db1.xml"/>
        <import resource="spring-db2.xml"/>
        <import resource="spring-db3.xml"/>
        <import resource="spring-dao.xml"/>
        <import resource="spring-dubbo.xml"/>
        
        <beans profile="default">
            <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
                  <property name="fileEncoding" value="utf-8"/>
                  <property name="location" value="classpath:config.properties"/>
            </bean>
        </beans>
        
        <beans profile="test">
            <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
                  <property name="fileEncoding" value="utf-8"/>
                  <property name="location" value="classpath:config-test.properties"/>
            </bean>
        </beans>
        
         <beans profile="online">
            <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
                  <property name="fileEncoding" value="utf-8"/>
                  <property name="location" value="classpath:config-online.properties"/>
            </bean>
        </beans>
     
    </beans>

    注意:profile的定义一定要在文档的最下边,否则会有异常

    激活profile:有多重方式,可以在web.xml中初始化参数,也可以添加JVM参数,junit单元测试的时候则使用注解

    web.xml方式如下

    <init-param>
      <param-name>spring.profiles.active</param-name>
      <param-value>online</param-value>
    </init-param>

    推荐JVM参数方式:建议这种方式设置,tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS

    set JAVA_OPTS="-Dspring.profiles.active=test"

    3.配置中心

    有了多环境的配置看起来已经非常完美了,事实如此。但是在一个企业当中,难免会有一大堆的项目,各种数据源,定时任务,缓存等等。运维的同学们表示:咱们的头很大,我的有道云笔记里面全是各种项目的记录,见鬼去吧!

    所以这里就不得不讲一下配置中心了。这里推荐一种:taobao diamond, 具体怎么用百度吧~~,因为它是一个独立的项目,这里我就不写了。

  • 相关阅读:
    一些对数学领域及数学研究的个人看法(转载自博士论坛wcboy)
    dynamic与var
    转载-V.I.Arnold, beyond a mathematician
    转载---青年问禅师
    转载-傅里叶级数的几何意义 – 巧妙记忆公式的方法
    转载--柯尔莫哥洛夫
    转载--黎曼
    动态规划复习
    几匹单调队列水题
    fastIO
  • 原文地址:https://www.cnblogs.com/itechpark/p/yinzei_properties.html
Copyright © 2011-2022 走看看