spring 的一个属性注入的问题

项目花园范德彪 2014-12-16 04:10:47
我的项目要使用两个数据源

这样的话,自然就出来的两套sessionfactory了.

<!-- 以下是mysql 的DAO -->

<bean id="quartsPlanDao" class="com.hpb.quarts.mysql.dao.impl.QuartsPlanDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryMysql"></property>
</bean>

<bean id="hpbMoneyInfoDao" class="com.hpb.quarts.mysql.dao.impl.HpbMoneyInfoDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryMysql"></property>
</bean>


------------------------
<!-- 以下是mysql 的DAO -->

<bean id="memAccessIssueRuleDao" class="com.hpb.quarts.oracle.dao.impl.MemAccessIssueRuleDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryOracle"></property>
</bean>

<bean id="sysIssuePlanDao" class="com.hpb.quarts.oracle.dao.impl.SysIssuePlanDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryOracle"></property>
</bean>

我怎么用component
或扫描的方式.
注入这个sqlsessionFactory

这样的话,就不用我每写一个DAO,要到这里来写的BEAN了.

他们的包 是不同的..

很久没登这论坛了.
没想到我有这么多的分.

...全文
300 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 zhangjihao 的回复:

@Repository("quartsPlanDao")
public class QuartsPlanDaoImpl implements QuartsPlanDao{
    @Resource
    SessionFactory sqlSessionFactoryMysql;
}

@Repository("sysIssuePlanDao")
public class SysIssuePlanDaoImpl implements SysIssuePlanDao{
    @Resource
    SessionFactory sqlSessionFactoryOracle;
}
Spring config xml:

<context:component-scan base-package="com.hpb.quarts"/>
不会这么简单吧,我理解错了?
你的思路应该是对的.. 开始的时候,我也是这样配置.. 但是,不知道为什么注入不进去. 而且,sqlsession 不是一个属性. 它只是那个support 的一个SET方法 . 我不理解,为什么就注入不进去了. 就是想用这样的扫描方式 但是我没试成功
  • 打赏
  • 举报
回复
引用 2 楼 whos2002110 的回复:
AbstractRoutingDataSource 配合aop 是实现多数据源切换.可以最大程度降低代码的耦合, 不管是service,dao本身都无需关注数据源问题. 不同的数据源仅仅是切点特征不同而以, 比如:service方法名. mysqlXXX 查询mysql数据源, oracleXXX查询oracle数据源
是的.事务的部分是这样配置 的. <aop:pointcut id="appService" expression="execution(* com.hpb..*.*MService*(..))" /> 因为他支持表达式.
  • 打赏
  • 举报
回复
引用 1 楼 wobuxiangnila 的回复:
想要不重复这么写,可以搞两个baseDao 继承来用
现在用的就是继承 .但是,父类是注入进去的. 子类,出来的时候,非spring对象.
  • 打赏
  • 举报
回复
多套SessionFactory必须是可以的;你用Spring3.x来做的话直接用注解把SessionFactory注入到各种Dao中即可。
  • 打赏
  • 举报
回复

<?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-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/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:applicationContext.properties</value>
			</list>
		</property>
	</bean>
	
	
		<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
		
	<context:component-scan base-package="com.jfpal" >
	</context:component-scan>
	
	<!-- 数据源 -->
	<bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="12" />
		<property name="maxIdleTime" value="1800" />
		<property name="acquireIncrement" value="2" />
		<property name="maxStatements" value="10" />
		<property name="initialPoolSize" value="2" />
		<property name="idleConnectionTestPeriod" value="1800" />
		<property name="acquireRetryAttempts" value="30" />
		<property name="acquireRetryDelay" value="100" />
		<property name="breakAfterAcquireFailure" value="false" />
		<property name="testConnectionOnCheckout" value="false" />
	</bean>
  
  
  <!-- 数据源2 -->
	<bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver2}"></property>
		<property name="jdbcUrl" value="${jdbc.url2}"></property>
		<property name="user" value="${jdbc.username2}"/>
		<property name="password" value="${jdbc.password2}"/>
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="12" />
		<property name="maxIdleTime" value="1800" />
		<property name="acquireIncrement" value="2" />
		<property name="maxStatements" value="10" />
		<property name="initialPoolSize" value="2" />
		<property name="idleConnectionTestPeriod" value="1800" />
		<property name="acquireRetryAttempts" value="30" />
		<property name="acquireRetryDelay" value="100" />
		<property name="breakAfterAcquireFailure" value="false" />
		<property name="testConnectionOnCheckout" value="false" />
	</bean>
	
	<bean id="dynamicDataSource" class="com.jfpal.framework.dao.DynamicDataSource" >  
    <!-- 通过key-value的形式来关联数据源 -->  
    <property name="targetDataSources">  
        <map>  
            <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>  
            <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>  
        </map>  
    </property>  
    <property name="defaultTargetDataSource" ref="dataSourceOne" />  
</bean> 
<bean id="dataSourceInterceptor" class="com.jfpal.framework.interceptor.DataSourceInterceptor">
</bean>
	<aop:config>
		<aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">  
            <aop:pointcut id="switch" expression="execution(* com.jfpal.pmout.payterminal.service.*.*.*(..))" /> 
            <!-- 进入方法前设置为数据源2 -->
            <aop:before pointcut-ref="switch" method="setdataSourceTwo" />
            <!-- 退出方法,设置回数据源1 -->  
            <aop:after pointcut-ref="switch" method="setdataSourceOne"/>
        </aop:aspect>
        
	</aop:config>
	
	<!-- SqlSessionFactory配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dynamicDataSource" />
		<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
		<property name="typeAliasesPackage" value="com.jfpal" />
		<!-- 显式指定Mapper文件位置 -->
		<property name="mapperLocations" value="classpath*:mybatis/**/*Mapper.xml" />
		<property name="configurationProperties">
			<props>
				<prop key="dialect">oracle</prop>
			</props>
		</property>
	</bean>

	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
	<!-- ================================事务相关控制=================================================    -->
  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
          <property name="dataSource" ref="dynamicDataSource"></property>

    </bean>     
  
  <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="select*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="find*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="get*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="save*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
      <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
                            rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
                                           
    </tx:attributes>
  </tx:advice>
  

</beans>

全部配置在这里了,具体数据库配置就不用了吧?
  • 打赏
  • 举报
回复
貌似我配过多个数据源,我看分来的~
howsun_zh 2014-12-16
  • 打赏
  • 举报
回复

@Repository("quartsPlanDao")
public class QuartsPlanDaoImpl implements QuartsPlanDao{
    @Resource
    SessionFactory sqlSessionFactoryMysql;
}

@Repository("sysIssuePlanDao")
public class SysIssuePlanDaoImpl implements SysIssuePlanDao{
    @Resource
    SessionFactory sqlSessionFactoryOracle;
}
Spring config xml:

<context:component-scan base-package="com.hpb.quarts"/>
不会这么简单吧,我理解错了?
whos2002110 2014-12-16
  • 打赏
  • 举报
回复
AbstractRoutingDataSource 配合aop 是实现多数据源切换.可以最大程度降低代码的耦合, 不管是service,dao本身都无需关注数据源问题. 不同的数据源仅仅是切点特征不同而以, 比如:service方法名. mysqlXXX 查询mysql数据源, oracleXXX查询oracle数据源
猎魔人-不纯 2014-12-16
  • 打赏
  • 举报
回复
想要不重复这么写,可以搞两个baseDao 继承来用

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧