<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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.xsd"> <!-- enable component scanning --> <!-- <import resource="classpath:applicationContext-ehcache.xml"/> --> <context:component-scan base-package="com.zeusjava" /> <!-- enable autowire --> <context:annotation-config /> <!-- enable transaction demarcation with annotations --> <tx:annotation-driven /> <!-- 读取mysql jdbc的配置--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- 配置数据源,从上面配置文件读取--> <!-- 数据源 --> <!--<bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource">--> <!--<property name="driverClassName" value="${jdbc.driverClassName}" />--> <!--<property name="url" value="${jdbc.url}" />--> <!--<property name="username" value="${jdbc.username}" />--> <!--<property name="password" value="${jdbc.password}" />--> <!--</bean>--> <!-- 第一个数据库 --> <bean id="dataSource" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="mysql/main" /> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="xaDataSourceProperties" value="URL=${jdbc.url};user=${jdbc.username};password=${jdbc.password}" /> <property name="exclusiveConnectionMode" value="true" /> <property name="connectionPoolSize" value="10" /> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <!-- 第二个数据库 --> <bean id="dataSourceB" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="mysql/news" /> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="xaDataSourceProperties" value="URL=${jdbc.url.b};user=${jdbc.username};password=${jdbc.password}" /> <property name="exclusiveConnectionMode" value="true" /> <property name="connectionPoolSize" value="10" /> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <bean id="sqlSessionFactoryB" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/mybatis-config-b.xml" /> <property name="dataSource" ref="dataSourceB" /> <property name="typeAliasesPackage" value="com.zeusjava.kernel.entity"/> <property name="mapperLocations"> <array> <value>classpath:mybatis/mapper/*.xml</value> </array> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置扫描Domain的包路径 --> <property name="typeAliasesPackage" value="com.zeusjava.kernel.entity"/> <!-- 配置mybatis配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!-- 配置扫描Mapper XML的位置 --> <!--<property name="mapperLocations" value="classpath:com/zeusjava/kernel/mapper/*Mapper.xml"/>--> <property name="mapperLocations"> <array> <value>classpath:mybatis/mapper/*.xml</value> </array> </property> </bean> <!-- 配置扫描Mapper接口的包路径 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zeusjava.kernel.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown"> <value>true</value> </property> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300" /> </bean> <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"> <ref bean="atomikosTransactionManager" /> </property> <property name="userTransaction"> <ref bean="atomikosUserTransaction" /> </property> </bean> <aop:aspectj-autoproxy /> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* *com.zeusjava.kernel.service..*(..))" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="springTransactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="has*" propagation="REQUIRED" read-only="true" /> <tx:method name="locate*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" /> </tx:attributes> </tx:advice> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface" value="com.zeusjava.kernel.mapper.UserMapper" /> </bean> <bean id="empMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactoryB" /> <property name="mapperInterface" value="com.zeusjava.kernel.mapper.EmpMapper" /> </bean> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> </beans>