spring配置檔案中分別使用多個properties檔案

yingxian_Fei發表於2017-03-31

在使用spring時,有時候需要為了模組配置方便有時候需要針對不同的模組建立不同的applicationContext的配置檔案,然後在對應模組的配置檔案中對相應的模組進行單獨配置。


1、載入不同模組的配置檔案

首先載入不同的配置檔案用於針對不同的模組配置:如

    applicationContext-db.xml  用於資料庫相關的配置

    applicationContext-interceptors.xml  用於攔截器相關的配置

    applicationContext-memcached.xml  用於memcached快取相關的配置

之後在web.xml指定新增所有指定字首的配置,如下指定載入所有“applicationContext-”字首的配置:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
  </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載入屬性檔案方法如下:

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
			<value>classpath:jdbc.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
	</bean> 	


如下為applicationContext-db.xml中的完整配置,applicationContext-memcached.xml中的配指定不哦那個的classpath為不同的屬性配置檔案即可:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  

	<!-- 載入資料庫屬性配置檔案 -->
 	<!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->
 	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
			<value>classpath:jdbc.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
	</bean> 	
	
	<!--  dataSource configuration -->
 	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="minPoolSize" value="10" />
		<property name="maxPoolSize" value="100" />
		<property name="acquireIncrement" value="3" />
		<property name="maxIdleTime" value="60" />
		<property name="checkoutTimeout" value="18000" />
		<property name="idleConnectionTestPeriod" value="180" />
	</bean>	
	
	<!-- sessionFactory configuration -->
 	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
		<!-- 自動掃描註解方式配置的hibernate類檔案 -->
		<property name="packagesToScan">
            <list>
                <!-- Add model package to here. -->
                <value>api.landsem.db.model</value>
            </list>
		</property>
        <property name="hibernateProperties">
			<value>
				hibernate.dialect=${hibernate.dialect}
				hibernate.query.substitutions=${hibernate.query.substitutions}
				hibernate.cache.provider_class=${hibernate.cache.provider_class}
				hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
				hibernate.show_sql=${hibernate.show_sql}
            </value>
        </property>		
	</bean> 
	
 	<bean id="passwordEncoder"
		class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" />
		
	<bean id="saltSource"
		class="org.springframework.security.authentication.dao.ReflectionSaltSource">
		<property name="userPropertyToUse" value="salt" />
	</bean>
	
	<!-- ==============add db dao package to here===================== -->
	<context:component-scan base-package="api.landsem.db.dao.impl" />
	
	<!-- ======================================Transactional configuration============================================= -->
	<!-- Enable @Transactional support -->
    <tx:annotation-driven transaction-manager="transactionManager"/> 
    
	<!-- 配置事務管理器 -->
 	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>  

	<!-- ===============Add db service package to here=================== -->
 	<context:component-scan base-package="api.landsem.db.service.impl" />
 	
	<!-- database handler. -->
	<context:component-scan base-package="api.landsem.db.handler.impl" /> 	 	
</beans>


相關文章