zoukankan      html  css  js  c++  java
  • spring配置文件中分别使用多个properties文件

    spring配置文件中分别使用多个properties文件

    在使用spring时,有时候需要为了模块配置方便有时候需要针对不同的模块建立不同的applicationContext的配置文件,然后在对应模块的配置文件中对相应的模块进行单独配置。

    1、加载不同模块的配置文件

    首先加载不同的配置文件用于针对不同的模块配置:如

        applicationContext-db.xml  用于数据库相关的配置

        applicationContext-interceptors.xml  用于拦截器相关的配置

        applicationContext-memcached.xml  用于memcached缓存相关的配置

    之后在web.xml指定添加所有指定前缀的配置,如下指定加载所有“applicationContext-”前缀的配置:

    [html] view plain copy
     
     print?
    1. <context-param>  
    2.   <param-name>contextConfigLocation</param-name>  
    3.   <param-value>classpath:applicationContext-*.xml</param-value>  
    4. </context-param>  

    2、加载使用它不同的properties文件

    不同配置文件中加载不同的属性配置文件并属性值,如

        applicationContext-db.xml  中加载jdbc.properties属性配置用于获取jdbc的相关配置信息。

        applicationContext-memcached.xml 中加载memcached.properties的属性文件用于获取memcached缓存的相关配置信息。

    实现上主要是使用spring提供的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer bean来在不同的xml配置中加载不同的属性配置文件,否则直接使用property-placeholder加载时第二个配置文件无法成功加载,xml无法获取到相关的配置。spring加载属性文件方法如下:

    [html] view plain copy
     
     print?
    1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    2.     <property name="locations">  
    3.         <list>  
    4.         <value>classpath:jdbc.properties</value>  
    5.         </list>  
    6.     </property>  
    7.     <property name="ignoreUnresolvablePlaceholders" value="true" />  
    8. </bean>     



    如下为applicationContext-db.xml中的完整配置,applicationContext-memcached.xml中的配指定不哦那个的classpath为不同的属性配置文件即可:

    [html] view plain copy
     
     print?
      1. <?xml version="1.0" encoding="UTF-8"?>    
      2. <beans xmlns="http://www.springframework.org/schema/beans"    
      3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      4.     xmlns:p="http://www.springframework.org/schema/p"  
      5.     xmlns:aop="http://www.springframework.org/schema/aop"   
      6.     xmlns:context="http://www.springframework.org/schema/context"  
      7.     xmlns:jee="http://www.springframework.org/schema/jee"  
      8.     xmlns:tx="http://www.springframework.org/schema/tx"  
      9.     xsi:schemaLocation="    
      10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
      11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
      12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
      13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd  
      14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">    
      15.   
      16.     <!-- 加载数据库属性配置文件 -->  
      17.     <!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->  
      18.       
      19.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
      20.         <property name="locations">  
      21.             <list>  
      22.             <value>classpath:jdbc.properties</value>  
      23.             </list>  
      24.         </property>  
      25.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
      26.     </bean>     
      27.       
      28.     <!--  dataSource configuration -->  
      29.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
      30.         <property name="jdbcUrl" value="${jdbc.url}" />  
      31.         <property name="driverClass" value="${jdbc.driverClassName}" />  
      32.         <property name="user" value="${jdbc.username}" />  
      33.         <property name="password" value="${jdbc.password}" />  
      34.         <property name="minPoolSize" value="10" />  
      35.         <property name="maxPoolSize" value="100" />  
      36.         <property name="acquireIncrement" value="3" />  
      37.         <property name="maxIdleTime" value="60" />  
      38.         <property name="checkoutTimeout" value="18000" />  
      39.         <property name="idleConnectionTestPeriod" value="180" />  
      40.     </bean>     
      41.       
      42.     <!-- sessionFactory configuration -->  
      43.     <bean id="sessionFactory"  
      44.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
      45.         <property name="dataSource">  
      46.             <ref bean="dataSource" />  
      47.         </property>  
      48.         <property name="configLocation" value="classpath:hibernate.cfg.xml"/>  
      49.         <!-- 自动扫描注解方式配置的hibernate类文件 -->  
      50.         <property name="packagesToScan">  
      51.             <list>  
      52.                 <!-- Add model package to here. -->  
      53.                 <value>api.landsem.db.model</value>  
      54.             </list>  
      55.         </property>  
      56.         <property name="hibernateProperties">  
      57.             <value>  
      58.                 hibernate.dialect=${hibernate.dialect}  
      59.                 hibernate.query.substitutions=${hibernate.query.substitutions}  
      60.                 hibernate.cache.provider_class=${hibernate.cache.provider_class}  
      61.                 hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}  
      62.                 hibernate.show_sql=${hibernate.show_sql}  
      63.             </value>  
      64.         </property>         
      65.     </bean>   
      66.       
      67.     <bean id="passwordEncoder"  
      68.         class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" />  
      69.           
      70.     <bean id="saltSource"  
      71.         class="org.springframework.security.authentication.dao.ReflectionSaltSource">  
      72.         <property name="userPropertyToUse" value="salt" />  
      73.     </bean>  
      74.       
      75.     <!-- ==============add db dao package to here===================== -->  
      76.     <context:component-scan base-package="api.landsem.db.dao.impl" />  
      77.       
      78.     <!-- ======================================Transactional configuration============================================= -->  
      79.     <!-- Enable @Transactional support -->  
      80.     <tx:annotation-driven transaction-manager="transactionManager"/>   
      81.       
      82.     <!-- 配置事务管理器 -->  
      83.     <bean id="transactionManager"  
      84.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
      85.         <property name="sessionFactory" ref="sessionFactory" />  
      86.     </bean>    
      87.   
      88.     <!-- ===============Add db service package to here=================== -->  
      89.     <context:component-scan base-package="api.landsem.db.service.impl" />  
      90.       
      91.     <!-- database handler. -->  
      92.     <context:component-scan base-package="api.landsem.db.handler.impl" />           
      93. </beans>  
  • 相关阅读:
    Final TFS 2008 Feature List
    来看看微软对测试是什么要求
    淘宝设计流程
    Disable try catch
    jQuery validate API
    iPhone手机开发平台入门介绍和教程
    VSSpeedster Speed up your VS 2010
    Where are the SDK tools? Where is ildasm?
    效率高的删除语句truncate table [tablename]
    修改Hosts去除各站广告
  • 原文地址:https://www.cnblogs.com/handsome1013/p/7472898.html
Copyright © 2011-2022 走看看