gpt4 book ai didi

java - appCtx 中的多个事务遇到 NoUniqueBeanDefinitionException

转载 作者:行者123 更新时间:2023-11-30 02:00:56 30 4
gpt4 key购买 nike

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

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

app-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;

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
DataSourceTransactionManager prayasTransactionManager;

@Autowired
DataSourceTransactionManager erpTransactionManager;

@Autowired
HRMSPickSalaryDataDAO hRMSPickSalaryDataDAO;
}

错误日志

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newPayrollService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.datasource.DataSourceTransactionManager com.awzpact.prayas.service.NewPayrollService.prayasTransactionManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.datasource.DataSourceTransactionManager] is defined: expected single matching bean but found 2: transactionManager,erptransactionManager
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:4020)
at org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:1012)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:381)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.datasource.DataSourceTransactionManager com.awzpact.prayas.service.NewPayrollService.prayasTransactionManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.datasource.DataSourceTransactionManager] is defined: expected single matching bean but found 2: transactionManager,erptransactionManager
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 44 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.datasource.DataSourceTransactionManager] is defined: expected single matching bean but found 2: transactionManager,erptransactionManager
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 46 more

我无法弄清楚,我将bean id隔离为,我还引用了其他数据源,即dataSourcePayroll .

请让我知道实现此目的的最佳解决方案,或者是否有其他解决方案?

最佳答案

其中一种方法是指定 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;
}

关于java - appCtx 中的多个事务遇到 NoUniqueBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868046/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com