appCtx中的多个事务遇到NoUniqueBeanDefinitionException

时间:2018-10-18 06:18:44

标签: java spring spring-mvc spring-transactions

我正在研究基于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隔离为一个,同时也引用了另一个datasource,即dataSourcePayroll

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

2 个答案:

答案 0 :(得分:1)

其中一种方法是指定咖啡豆的名称

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

答案 1 :(得分:1)

问题是您在Bean定义中获得了不同的Bean ID,并且正在使用具有不同名称的该属性。因此,Spring容器无法识别为prayasTransactionManagererpTransactionManager ..

分配了哪个bean。

解决方案是将相同的bean id用于bean定义,用作Property name。 在您的情况下:

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

P.S:给出bean ID和属性名(Dependency)的最佳实践是使用camelCase模式和有意义的名字。