【问题标题】:Spring Boot Integration Test - BeanDefinitionOverrideExceptionSpring Boot 集成测试 - BeanDefinitionOverrideException
【发布时间】:2020-04-24 05:19:39
【问题描述】:

我有一个 Spring Boot 集成测试,其类定义如下。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestApplication.class})
@Transactional
public class AuthorisationManagerControllerRequestIT
{
...

我得到以下异常:

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'org.springframework.transaction.config.internalTransactionAdvisor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration; factoryMethodName=transactionAdvisor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]] for bean 'org.springframework.transaction.config.internalTransactionAdvisor': There is already [Root bean: class [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.

我的TestApplication类定义如下:

@SpringBootApplication
@ComponentScan(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Application.class)
})
@ImportResource({
        "classpath:properties-context.xml"          
})
public class TestApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(TestApplication.class, args);
    }
}

从properties-context.xml读取,定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/util
                            http://www.springframework.org/schema/util/spring-util.xsd">

    <context:property-placeholder location="${CONFIG.ROOT}/external.properties"/>


</beans>

这从名为 external.properties 的文件中读取。我在我的外部属性文件中设置了属性“spring.main.allow-bean-definition-overriding=true”。我添加了这个,因为当我在出现这个相同的异常之前运行应用程序时,但添加这个之后不再出现。

需要什么来阻止此异常出现在我的集成测试中?

【问题讨论】:

    标签: spring-boot spring-boot-test


    【解决方案1】:

    好吧,鉴于您的错误,这似乎合乎逻辑: org.springframework.beans.factory.support.BeanDefinitionOverrideException 在 spring 创建一个与已经创建的同名的 bean 时抛出。 Spring-boot 2 默认关闭的属性spring.main.allow-bean-definition-overriding=true 允许您覆盖bean。

    我不明白为什么你有 2 个 bean 在事务方面相同...

    【讨论】:

      猜你喜欢
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2017-10-27
      • 1970-01-01
      • 2017-06-28
      • 2015-10-16
      • 1970-01-01
      相关资源
      最近更新 更多