spring读写分离

主要配置:

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!-- 启动扫描component功能 -->
	<context:component-scan base-package="com.tch.test.springmvcmybatis" />
	<!-- viewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<mvc:annotation-driven />
	<!-- 拦截器 -->
	<!-- <mvc:interceptors> 这里配置的拦截器相当于全局拦截器,只要有响应的后端处理器,就会经过该拦截器 <bean class="com.tch.test.springmvcmybatis.interceptor.MyInteceptor" 
		/> <mvc:interceptor> 只拦截匹配的路径 <mvc:mapping path="/namespace/*" /> <bean class="com.tch.test.springmvcmybatis.interceptor.MyInteceptor2" 
		/> </mvc:interceptor> </mvc:interceptors> -->

	<!-- 启动注解实物配置功能 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
	<!-- 数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<!-- 数据源(slave) -->
	<bean id="slave_dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test_slave"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>

	<bean id="dynamicDataSource"
		class="com.tch.test.springmvcmybatis.datasource.DynamicDataSource">
		<property name="targetDataSources">
			<map>
				<entry key="slave" value-ref="slave_dataSource" />
			</map>
		</property>
		<property name="defaultTargetDataSource" ref="dataSource" />
	</bean>

	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dynamicDataSource" />
	</bean>

	<bean id="chooseDataSourceAdvice"
		class="com.tch.test.springmvcmybatis.datasource.ChooseDataSource"></bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

	<aop:config expose-proxy="true"> <!-- proxy-target-class="true" -->
		<!-- 只对业务逻辑层实施事务 -->
		<aop:pointcut id="txPointcut"
			expression="execution(* com.tch.test.springmvcmybatis.service.*Service.*(..)) AND ! execution(* com.tch.test.springmvcmybatis.service.*Service.get*(..))" />
		<aop:pointcut id="determineReadOrWritePointcut"
			expression="execution(* com.tch.test.springmvcmybatis.service.*Service.get*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />

		<!-- 通过AOP切面实现读/写库选择 -->
		<aop:aspect order="-2147483648" ref="chooseDataSourceAdvice">
			<aop:around pointcut-ref="determineReadOrWritePointcut"
				method="determineReadOrWriteDB" />
		</aop:aspect>
	</aop:config>

	<!--读取数据库配置文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dynamicDataSource" />
		<!-- 如果mapper的xml文件名字和mapper接口的名字一致,并且在同一目录下(例如UserMapper.java和UserMapper.xml),可以不配置mapperLocations, -->
		<property name="mapperLocations" value="classpath:mybatis/**/*Mapper.xml" />
		<!-- 指定别名,在mapper的xml文件中可以使用别名(例如User/user来代表com.tch.test.spring_mybatis.entity.User),提高开发效率 -->
		<!-- <property name="typeAliasesPackage" value="com.tch.test.springmvcmybatis.bean" 
			/> -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定mapper接口包名,可以通过动态代理生成代理类,这样就可以在service层直接通过注入的方式进行dao操作了 -->
		<property name="basePackage" value="com.tch.test.springmvcmybatis.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

</beans>

说明:

当满足execution(* com.tch.test.springmvcmybatis.service.*Service.get*(..))切面的时候,切面会调用com.tch.test.springmvcmybatis.datasource.ChooseDataSource的determineReadOrWriteDB方法,

这个切面是around的类型,首先通过JdbcContextHolder.setSlave();将当前线程的数据源设为slave,最终获取到的dataSource就是slave_dataSource,从而达到对读操作使用slave_dataSource数据源的目的。

当满足execution(* com.tch.test.springmvcmybatis.service.*Service.*(..)) AND ! execution(* com.tch.test.springmvcmybatis.service.*Service.get*(..))切面的时候,

也就是增删改操作,需要事务,也就需要使用dataSource作为数据源,因为这时候没有调用JdbcContextHolder.setSlave(),此时,com.tch.test.springmvcmybatis.datasource.DynamicDataSource

就会使用默认的数据源defaultTargetDataSource,即dataSource作为数据源,从而达到目的。

这里面要注意的是,transactionManager和sqlSessionFactory都需要使用dynamicDataSource作为数据源,dynamicDataSource会根据determineCurrentLookupKey方法的返回值决定使用哪个数据源(slave_dataSource或者dataSource)

order="-2147483648" 表明优先级最高

猜你喜欢

转载自dreamoftch.iteye.com/blog/2231118