读取多个配置文件

配置文件目录
在这里插入图片描述
读取多个配置
1. db.properties 数据库连接配置

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mr_05
user=mr_1808
password=1

2.redis.properties 数据连接配置

redis.maxIdle=300
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=127.0.0.1
redis.port=6379
redis.password=123

在 applicationContext.xml 中添加如下配置

<!-- 将多个配置文件位置放到列表中 -->
	<bean id="propertyResources" class="java.util.ArrayList">
		<constructor-arg>
			<list>
				<!-- 这里支持多种寻址方式:classpath和file  这里用的classpath-->
				<value>classpath:redis.properties</value>
				<value>classpath:db.properties</value>
			</list>
		</constructor-arg>
	</bean>
	<!-- 将配置文件读取到容器中,交给Spring管理 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" ref="propertyResources">

		</property>
	</bean>

配置完成后的 applicationContext.xml 配置文件如下

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

	<context:component-scan base-package="com.mr">
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!-- 加载 db.properties 文件
	<context:property-placeholder location="classpath:db.properties"/>

	<context:property-placeholder location="classpath:redis.properties"/>
-->
	<!-- 将多个配置文件位置放到列表中 -->
	<bean id="propertyResources" class="java.util.ArrayList">
		<constructor-arg>
			<list>
				<!-- 这里支持多种寻址方式:classpath和file  这里用的classpath-->
				<value>classpath:redis.properties</value>
				<value>classpath:db.properties</value>
			</list>
		</constructor-arg>
	</bean>
	<!-- 将配置文件读取到容器中,交给Spring管理 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" ref="propertyResources">

		</property>
	</bean>

	<!--dataSource  -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${user}"></property>
		<property name="password" value="${password}"></property>
	</bean>

	<!--配置整合mybatis的核心类 SqlSessionFactoryBean -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载mybatis-config.xml文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--
		<property name="configLocation" value="classpath:spring-redis.xml"></property>
-->
		<!-- 加载映射文件 -->
		<property name="mapperLocations" value="classpath:com/mr/mapper/*.xml"></property>
	</bean>
	
	<!-- 扫描mapper接口文件 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.mr.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
	</bean>
	
	<!-- 事务  -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 配置事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="txManager"><!-- 配置事务管理器 -->
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="login*" propagation="REQUIRED" rollback-for="Exception"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	 <!--proxy-target-class="true"-->
	<aop:config>
    	<aop:advisor pointcut="execution(* com.mr.service..*.*(..))" advice-ref="txAdvice"/>
    </aop:config>
	
</beans>

猜你喜欢

转载自blog.csdn.net/weixin_44100455/article/details/87877677
今日推荐