<?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <context:annotation-config /> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <tx:jta-transaction-manager /> <context:component-scan base-package="*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc_oracle_10g.properties</value> </property> </bean> <!-- 引入property文件 end --> <!-- 配置多个数据源 start --> <bean id="test1" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>test1</value> </property> <property name="xaDataSourceClassName"> <value>${test1.jdbc.class}</value> </property> <property name="xaProperties"> <props> <prop key="user">${test1.jdbc.username}</prop> <prop key="password">${test1.jdbc.password}</prop> <prop key="URL">${test1.jdbc.url}</prop> </props> </property> <property name="poolSize"> <value>3</value> </property> </bean> <bean id="test2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>test2</value> </property> <property name="xaDataSourceClassName"> <value>${test2.jdbc.class}</value> </property> <property name="xaProperties"> <props> <prop key="user">${test2.jdbc.username}</prop> <prop key="password">${test2.jdbc.password}</prop> <prop key="URL">${test2.jdbc.url}</prop> </props> </property> <property name="poolSize"> <value>1</value> </property> </bean> <!-- 配置多个数据源 end --> <!-- 为每个数据源配置相应的SessionFactory start --> <bean id="test1SessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="test1" /> <property name="packagesToScan" value="model" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.connection.release_mode">after_transaction</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext </prop> </props> </property> </bean> <bean id="test2SessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="test2" /> <property name="packagesToScan" value="model" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.connection.release_mode">after_transaction</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext </prop> </props> </property> </bean> <!-- 为每个数据源配置相应的SessionFactory end --> <!-- 配置动态可切换的sessionFactoryContent start --> <bean id="sessionFactoryContent" class="dao.session.impl.DynamicSessionFactoryImpl"> <property name="defaultTargetSessionFactory" ref="test1SessionFactoryBean" /> <property name="targetSessionFactorys"> <map> <entry value-ref="test1SessionFactoryBean" key="test1" /> <entry value-ref="test2SessionFactoryBean" key="test2" /> </map> </property> </bean> <!-- 配置动态可切换的sessionFactoryContent end --> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true" /> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300" /> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception,RuntimeException,exception.DBException" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> </aop:config> </beans>
以上是关键代码<applicationContext_dao.xml>
附件中有全部关键代码,有需要的童鞋请参考