annotation + hibernate + sping 配置二个数据源问题

CoCoAve 发布于 2011/07/16 13:37
阅读 805
收藏 0

代码编译没有问题,但dao1和dao2 最终都跑到第一个数据源上了,

不知道是什么原因,各位高手指点下,谢谢!

 

sping

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

    <context:property-placeholder location="classpath:application.properties" />
    <context:component-scan base-package="com.ft.user" />

    <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
        <property name="alias" value="mssql" />
        <property name="driver" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="driverUrl" value="jdbc:jtds:sqlserver://127.0.0.1:1433/shanghai;SelectMethod=cursor" />
        <property name="user" value="${dataSource.user}" />
        <property name="password" value="${dataSource.password}" />
        <property name="minimumConnectionCount" value="${dataSource.minPoolSize}" />
        <property name="maximumConnectionCount" value="${dataSource.maxPoolSize}" />
        <property name="maximumActiveTime" value="${dataSource.maxActiveTime}" />
        <property name="statistics" value="1m,15m,1d" />
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
                                <prop key="hibernate.collection.isolation">${hibernate.collection.isolation}</prop>
                
                <prop key="hibernate.memcached.servers">127.0.0.1:11210</prop>
                <prop key="hibernate.cache.region_prefix">quality.cache.ehcache</prop>                <prop key="hibernate.cache.use_structured_entries">true</prop>
                
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
                
            
            </props>
        </property>
        <property name="packagesToScan" value="com.ft.user.model" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- spring 事务管理 -->
    <tx:advice id="TxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- spring aop 管理 service-->
    <aop:config>
        <aop:pointcut id="ServiceOperation" expression="execution(* com.ft.user.service.*.*(..))" />
        <aop:advisor advice-ref="TxAdvice" pointcut-ref="ServiceOperation" />
    </aop:config>
    
    
    
    
    <!-- UCenter hibernate 配置 开始  -->
    <bean id="ucDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
        <property name="alias" value="mssql" />
        <property name="driver" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="driverUrl" value="jdbc:jtds:sqlserver://127.0.0.1:1433/ucenter002;SelectMethod=cursor" />
        <property name="user" value="sa" />
        <property name="password" value="123456" />
        <property name="minimumConnectionCount" value="20" />
        <property name="maximumConnectionCount" value="200" />
        <property name="maximumActiveTime" value="1200000" />
        <property name="statistics" value="1m,15m,1d" />
    </bean>
    <bean id="ucSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="ucDataSource" />
        <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
                                <prop key="hibernate.collection.isolation">${hibernate.collection.isolation}</prop>
                <prop key="hibernate.memcached.servers">127.0.0.1:11211</prop>
                <prop key="hibernate.cache.region_prefix">quality.cache.ehcache</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.ft.user.ucenter.model" />
    </bean>
    <bean id="ucTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="ucSessionFactory" />
    </bean>
    <!-- spring 事务管理 -->
    <tx:advice id="ucTxAdvice" transaction-manager="ucTransactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- spring aop 管理 service-->
    <aop:config>
        <aop:pointcut id="ucServiceOperation" expression="execution(* com.ft.user.ucenter.service.*.*(..))" />
        <aop:advisor advice-ref="ucTxAdvice" pointcut-ref="ucServiceOperation" />
    </aop:config>
    <!-- UCenter hibernate 配置 结束 -->
</beans>


 

dao1

        @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
         @Autowired
    public void setHibernateTemplate(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
        
       @Autowired
    public void setJdbcTemplate(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }


 


dao2

         @Autowired
    public void setSessionFactory(@Qualifier("ucSessionFactory")SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
         @Autowired
    public void setHibernateTemplate(@Qualifier("ucSessionFactory")SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
        
       @Autowired
    public void setJdbcTemplate(@Qualifier("ucDataSource")DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
加载中
返回顶部
顶部