spring配置文件中分别使用多个properties文件
来源:互联网 发布:js readonly 设置 编辑:程序博客网 时间:2024/06/05 17:53
在使用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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsdhttp://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>
- spring配置文件中分别使用多个properties文件
- Spring中配置文件读取多个Properties文件
- 在Spring配置文件中使用properties文件
- Spring 使用Properties配置文件
- 多个module在一个工程中分别debug
- Spring配置文件加载properties文件
- spring配置文件读取properties文件
- spring配置文件获取其他properties文件中的值及使用
- Spring/Maven/MyBatis配置文件结合properties文件使用
- Spring 使用Properties配置文件(一)
- Spring 使用Properties配置文件(二)
- spring 装载多个properties文件
- spring引入多个properties文件
- Spring配置多个properties文件写法
- Spring导入多个独立的 .properties配置文件
- Spring Xml配置文件自动加载properties文件
- spring配置文件中配置jdbc.properties 文件
- spring 配置文件中 引入properties文件
- 完整java开发中JDBC连接数据库代码和步骤
- 仿京东的一个MVC框架的使用
- includeos 创建 输出一个hello world的过程
- android studio NDK使用,编译c生成.so实践记录
- 分享一款很实用的数据恢复软件 easyrecovery
- spring配置文件中分别使用多个properties文件
- MySQL5.7 运行SELECT DISTINCT 遇到3065问题
- Linux下快速查看CPU使用情况的相关命令
- 前端框架与库的区别
- 数据库连接错误The Network Adapter could not establish the connection
- Required int[] parameter 'ids' is not present
- 几款移动跨平台App开发框架比较
- android多国语言文件夹汇总
- mysql运行sql文件出错