SSH2框架 在spring的配置文件ApplicationContext.xml里,配置了多个sessionFactory,代码里通过什么样的方式调用呢?
网上好多都是new ApplicationContext,然后测试。我不想用这样的测试类有实例代码最好(.java文件中的)
1. 不用显式的 new ApplicationContext的方式;
2. service层,DAO层调用的方式,针对每个请求取用数据源的变化,根据不同的SessionFactory名称,通过不同的方式切换取用SessionFactory。对hibernate不熟,大家帮忙看下 谢谢

解决方案 »

  1.   


    <?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:aop="http://www.springframework.org/schema/aop"    
        xmlns:tx="http://www.springframework.org/schema/tx"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd    
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
      
        <description>springJTA</description>  
      
        <!--指定Spring配置中用到的属性文件    
        <bean id="propertyConfig"    
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
            <property name="locations">    
                <list>    
                    <value>classpath:jdbc.properties</value>    
                </list>    
            </property>    
        </bean>       
        -->
        <!-- JOTM实例 -->  
        <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean">  
              <property name="defaultTimeout" value="500000"/>  
        </bean>  
      
        <!-- JTA事务管理器 -->  
        <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">      
            <property name="userTransaction" ref="jotm" />      
        </bean>  
      
        <!-- 数据源A -->    
        <bean id="dataSourceA" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">    
           <property name="dataSource">    
               <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">    
                   <property name="transactionManager" ref="jotm"/>    
                   <property name="driverName" value="com.mysql.jdbc.Driver"/>    
                   <property name="url" value="jdbc:mysql://localhost:3306/emp_mvc"/>    
               </bean>    
           </property>    
           <property name="user" value="root"/>    
           <property name="password" value="123456"/>    
        </bean>    
      
        <!-- 数据源B -->    
        <bean id="dataSourceB" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">    
           <property name="dataSource">    
               <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">    
                   <property name="transactionManager" ref="jotm"/>    
                   <property name="driverName" value="com.mysql.jdbc.Driver"/>    
                   <property name="url" value="jdbc:mysql://localhost:3306/emp_mvc2"/>    
               </bean>    
           </property>    
           <property name="user" value="root"/>    
           <property name="password" value="123456"/>    
        </bean>    
     
        <bean id = "sessionFactoryA"    
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
             <property name = "dataSource" ref="dataSourceA"/>
             <property name="mappingResources">
     <list>
       <value>com/ouku/JOTM/entity/Emp.hbm.xml</value>
     </list>
     </property>    
        </bean>  
           
        <bean id = "sessionFactoryB"    
             class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
             <property name = "dataSource" ref="dataSourceB"/>
             <property name="mappingResources">
     <list>
       <value>com/ouku/JOTM/entity/Emp2.hbm.xml</value>
     </list>
     </property>    
        </bean>       
      
        <!-- 事务切面配置 -->    
        <aop:config>    
            <aop:pointcut id="pointCut"  
                    expression="execution(* com.ouku.JOTM..*.*(..))"/><!-- 包及其子包下的所有方法 -->  
            <aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice"/>    
              
            <aop:advisor pointcut="execution(* *..ouku.JOTM.*.*(..))" advice-ref="txAdvice"/>  
        </aop:config>    
      
        <!-- 通知配置 -->    
        <tx:advice id="txAdvice" transaction-manager="jtaTransactionManager">    
           <tx:attributes>    
              <tx:method name="delete*" rollback-for="Exception"/>    
              <tx:method name="save*" rollback-for="Exception"/>    
              <tx:method name="update*" rollback-for="Exception"/>    
              <tx:method name="find*" read-only="true" rollback-for="Exception"/>  
              <tx:method name="*" read-only="true" rollback-for="Exception"/>    
           </tx:attributes>    
        </tx:advice>    
      
        <bean id="genericDao"    
                class="com.ouku.JOTM.DAO.GenericDaoImpl" autowire="byName"> 
                <property name="sessionFactoryA" ref="sessionFactoryA"></property> 
                <property name="sessionFactoryB" ref="sessionFactoryB"></property> 
        </bean>  
      
        <bean id="userService"    
                class="com.ouku.JOTM.biz.UserServiceImpl" autowire="byName">  
        </bean>  
      
    </beans>