- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发基于 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/
SpringMVC 应用程序使用最新的 jars。花了很长时间试图找出这篇文章失败的问题。如果有人遇到过这个问题,请您能回复一下 错误堆栈 INFO : org.springframework.bea
我正在使用 Spring 3.2.2,并且想知道是否有一种方法可以按类类型注入(inject) bean,而无需显式为其提供字符串名称。例如: @Named public MyClass{ } @Na
Spring 新手,现在正在学习依赖注入(inject)。我已经知道 Qualifier 注解用于区分不同的依赖关系。但是以下代码不起作用。 @Bean @Qualifier("tay
我正在使用 Spring boot,我定义了 spring.datasource.* 属性来启用我的数据源。如果我只使用它,它就可以正常工作。但是,我现在也尝试使用以下配置将 JMS 添加到我的应用程
尝试使用 Autowiring 两个 bean 时出现以下错误 No qualifying bean of type [javax.jms.ConnectionFactory] is defined:
我有一个带有 Spring 的 Maven 多模块项目。我有一个核心项目,其中包含所有数据访问 bean 以及entityManagerFactoryBean。 现在我有另一个 Maven 模块项目,
我正在开发基于 Spring MVC 的 Web 应用程序,其中我有两个数据源,为此我需要维护两个事务 **DataSourceTransactionManager**, 我什么也没做,只是复制了我的
问题 我收到错误 Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying
我正在尝试为我的 @Component 类创建一个具有继承的结构,以便重用一些通用代码。但我收到运行时错误: Caused by: org.springframework.beans.factory.
我有基于 SpringMVC 的 webApplication,所有 bean 都是通过注解定义的。今天我尝试为我的 Controller 编写测试。我放了第一个并尝试模拟此 Controller 中
我有一个 SpringBoot main/Application.java 类 @SpringBootApplication @ComponentScan(value = "com.nfl.dm.sh
我将下面的 bean 定义为 A.jar 的一部分 package abc; @Component public class ParentInterceptor implements ClientIn
我正在使用 Spring boot 和 Spring batch。我已经定义了不止一项工作。 我正在尝试构建 junit 来测试作业中的特定任务。 因此,我正在使用 JobLauncherTestUt
我有这个父类(super class): @Component public class DAOBase { } 另一个类扩展了 DAOBase @Component public class Voc
我正在开发一个解决方案来在 SmsService 中实现多个提供程序。我需要一种从我的上下文中注入(inject)策略类的方法。问题是选择取决于外部属性。当我尝试部署此代码时,我得到 org.spr
是否可以在构造函数中将泛型类型 Autowiring 到不同的泛型类型中?我目前的结构如下: @Service public class ExampleService { @Autowired Ser
我最近发布了一个有关多个数据源的问题。一切顺利,直到我遇到这个问题: 控制者 def doSomething() { def user=userService.getCurrentUser(
在Spring Boot和Mybatis项目中配置多个数据源时,出现以下异常: org.springframework.beans.factory.NoUniqueBeanDefinitionExce
我正在编写一个需要多种身份验证机制(基本、x509 和匿名)的 RESTful Web 服务。因此我有三个 三个独立的 spring 上下文文件中的元素。 当我启动我的服务时,出现以下异常: org.
我在使用@MockBean 注释时遇到问题。文档说 MockBean 可以替换上下文中的 bean,但我在单元测试中遇到了 NoUniqueBeanDefinitionException。我看不到如何
我是一名优秀的程序员,十分优秀!