2 回答
TA贡献1807条经验 获得超9个赞
其中一种方法是指定 bean 的名称
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" name="oldOne">
<property name="dataSource" ref="dataSource" />
</bean>
<!----- NEWLY ADDED Txn-------->
<bean id="erptransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" name="newOne">
<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;
public void getSalarayData(final String yearMonth, final String regionId, final String circleId, final Userdetail loginUser) {
final String tableSuffix = yearMonth.substring(4, 6) + yearMonth.substring(0, 4);
final TransactionStatus erpTransaction = erpTransactionManager.getTransaction(erpTxnDefination);
try {
List<SalaryDetailReport> list = hRMSPickSalaryDataDAO.findAll(yearMonth, regionId, circleId);
} catch (Exception e) {
}
final TransactionStatus prayasTransaction = prayasTransactionManager.getTransaction(prayasTxnDefination);
}
@Autowired
@Qualifier("oldOne")
DataSourceTransactionManager prayasTransactionManager;
@Autowired
@Qualifier("newOne")
DataSourceTransactionManager erpTransactionManager;
@Autowired
HRMSPickSalaryDataDAO hRMSPickSalaryDataDAO;
}
TA贡献1866条经验 获得超5个赞
问题是您在 bean 定义中获得了不同的 bean id,并且您正在使用具有不同名称的该属性。因此 Spring 容器无法识别分配给哪个 beanprayasTransactionManager 和erpTransactionManager..
解决方案是对用作属性名称的 bean 定义使用相同的 bean id。在你的情况下:
<!----- EXISTING Txn-------->
<bean id="prayasTransactionManager" 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>
或者像这样使用你的旧代码。
@Autowired
@Qualifier("transactionManager")
DataSourceTransactionManager prayasTransactionManager;
@Autowired
@Qualifier("erptransactionManager")
DataSourceTransactionManager erpTransactionManager;
PS:给出 bean id 和属性名称(依赖项)的最佳做法是使用带有有意义名称的驼峰命名法。
添加回答
举报