大家好,我在Spring MVC工作,我有一个问题。

我有这个bean定义的交易:

<tx:annotation-driven transaction-manager="txManager"/>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>


但是当我运行我的应用程序时,我遇到了以下异常:


  2019-05-24 18:14:51调试o.s.b.d.LoggingFailureAnalysisReporter-
  由于出现异常,应用程序无法启动
  org.springframework.beans.factory.support.BeanDefinitionOverrideException:
  名称无效的Bean定义
  'org.springframework.transaction.config.internalTransactionAdvisor'
  在类路径资源中定义
  [org / springframework / transaction / annotation / ProxyTransactionManagementConfiguration.class]:
  无法注册bean定义[root bean:class [null]; scope =;
  abstract = false; lazyInit = false; autowireMode = 3; dependencyCheck = 0;
  autowireCandidate = true; primary = false;
  factoryBeanName = org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration;
  factoryMethodName = transactionAdvisor; initMethodName = null;
  destroyMethodName =(推断);在类路径资源中定义
  [org / springframework / transaction / annotation / ProxyTransactionManagementConfiguration.class]]
  对于豆
  'org.springframework.transaction.config.internalTransactionAdvisor':
  已经有[Root bean:class
  [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor];
  scope =; abstract = false; lazyInit = false; autowireMode = 0;
  dependencyCheck = 0; autowireCandidate = true; primary = false;
  factoryBeanName = null; factoryMethodName = null; initMethodName = null;
  destroyMethodName = null]绑定。在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:897)
    在
  org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:274)
    在
  org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141)
    在
  org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117)
    在
  org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327)
    在
  org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
    在
  org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
    在
  org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
    在
  org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
    在
  org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
    在
  org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
    在
  org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    在
  org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    在
  org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    在
  org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    在
  org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)


当我评论tx:annotation-drive时,一切都很好,但是带有@Transactional注释的方法不起作用。我究竟做错了什么?

我的gradle依赖项:

compile group: 'org.springframework', name: 'spring-web', version: '5.1.5.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.5.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.3.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.5.RELEASE'

最佳答案

您需要做的是启用Bean Overriding。从Spring 2.1开始,此功能默认为禁用。您将得到一个异常,因为已经定义了一个具有相同名称的bean。

Spring 2.1发行说明摘录:Release note 2.1 - Bean Overriding

长话短说,您需要将此添加到您的application.properties文件中:

spring.main.allow-bean-definition-overriding=true


或以yaml:

spring:
   main:
     allow-bean-definition-overriding: true

关于java - 为什么以TX注释驱动会产生冲突?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56295454/

10-10 15:50