appCtx 中的多个事务遇到 NoUniqueBeanDefinitionException

我正在研究基于 Spring MVC 的 Web 应用程序,其中有两个数据源,我需要维护两个事务**DataSourceTransactionManager**,


我什么也没做,只是复制了我的EXISTING Txn交易片段并根据其他数据源对其进行了修改,请查看我的


应用程序-ctx.xml


<!----- EXISTING Txn-------->

     <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

            <property name="dataSource" ref="dataSource" />


        </bean> 

     <!----- NEWLY ADDED Txn-------->   

        <bean id="erptransactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

            <property name="dataSource" ref="dataSourcePayroll" />

        </bean> 

现在是服务类,正是我需要使用的地方。


package com.awzpact.prayas.service;


import com.awzpact.prayas.dao.HRMSPickSalaryDataDAO;

import com.awzpact.uam.domain.SalaryDetailReport;

import com.awzpact.uam.domain.Userdetail;

import com.awzpact.uam.exceptions.MyExceptionHandler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import org.springframework.stereotype.Service;

import org.springframework.transaction.TransactionDefinition;

import org.springframework.transaction.TransactionStatus;

import org.springframework.transaction.support.DefaultTransactionDefinition;


/**

 *

 * @author jack

 */

@Service

public class NewPayrollService {


    final TransactionDefinition erpTxnDefination = new DefaultTransactionDefinition();

    final TransactionDefinition prayasTxnDefination = new DefaultTransactionDefinition();

    final int BATCH_SIZE = 500;



桃花长相依
浏览 221回答 2
2回答

函数式编程

其中一种方法是指定 bean 的名称<bean id="transactionManager"&nbsp; class="org.springframework.jdbc.datasource.DataSourceTransactionManager" name="oldOne">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSource" />&nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp;&nbsp; &nbsp; &nbsp;<!----- NEWLY ADDED Txn-------->&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <bean id="erptransactionManager"&nbsp; class="org.springframework.jdbc.datasource.DataSourceTransactionManager" name="newOne">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSourcePayroll" />&nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp;然后使用限定符package com.awzpact.prayas.service;import com.awzpact.prayas.dao.HRMSPickSalaryDataDAO;import com.awzpact.uam.domain.SalaryDetailReport;import com.awzpact.uam.domain.Userdetail;import com.awzpact.uam.exceptions.MyExceptionHandler;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.stereotype.Service;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;/**&nbsp;*&nbsp;* @author jack&nbsp;*/@Servicepublic class NewPayrollService {&nbsp; &nbsp; final TransactionDefinition erpTxnDefination = new DefaultTransactionDefinition();&nbsp; &nbsp; final TransactionDefinition prayasTxnDefination = new DefaultTransactionDefinition();&nbsp; &nbsp; final int BATCH_SIZE = 500;&nbsp; &nbsp; public void getSalarayData(final String yearMonth, final String regionId, final String circleId, final Userdetail loginUser) {&nbsp; &nbsp; &nbsp; &nbsp; final String tableSuffix = yearMonth.substring(4, 6) + yearMonth.substring(0, 4);&nbsp; &nbsp; &nbsp; &nbsp; final TransactionStatus erpTransaction = erpTransactionManager.getTransaction(erpTxnDefination);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<SalaryDetailReport> list = hRMSPickSalaryDataDAO.findAll(yearMonth, regionId, circleId);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; final TransactionStatus prayasTransaction = prayasTransactionManager.getTransaction(prayasTxnDefination);&nbsp; &nbsp; }&nbsp; &nbsp; @Autowired&nbsp; &nbsp; @Qualifier("oldOne")&nbsp; &nbsp; DataSourceTransactionManager prayasTransactionManager;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; @Qualifier("newOne")&nbsp; &nbsp; DataSourceTransactionManager erpTransactionManager;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; HRMSPickSalaryDataDAO hRMSPickSalaryDataDAO;}

心有法竹

问题是您在 bean 定义中获得了不同的 bean id,并且您正在使用具有不同名称的该属性。因此 Spring 容器无法识别分配给哪个 beanprayasTransactionManager 和erpTransactionManager..解决方案是对用作属性名称的 bean 定义使用相同的 bean id。在你的情况下:<!----- EXISTING Txn-------->&nbsp;<bean id="prayasTransactionManager"&nbsp; class="org.springframework.jdbc.datasource.DataSourceTransactionManager">&nbsp; &nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSource" />&nbsp; &nbsp; </bean>&nbsp;&nbsp;<!----- NEWLY ADDED Txn-------->&nbsp; &nbsp;&nbsp; &nbsp; <bean id="erpTransactionManager"&nbsp; class="org.springframework.jdbc.datasource.DataSourceTransactionManager">&nbsp; &nbsp; &nbsp; &nbsp; <property name="dataSource" ref="dataSourcePayroll" />&nbsp; &nbsp; </bean>或者像这样使用你的旧代码。&nbsp; @Autowired@Qualifier("transactionManager")DataSourceTransactionManager prayasTransactionManager;@Autowired@Qualifier("erptransactionManager")DataSourceTransactionManager erpTransactionManager;PS:给出 bean id 和属性名称(依赖项)的最佳做法是使用带有有意义名称的驼峰命名法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java