Spring重写主bean与非主bean [英] Spring overriding primary bean with non-primary bean

查看:656
本文介绍了Spring重写主bean与非主bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用@Primary在测试配置中声明的测试期间覆盖Spring bean。一个声明在src / main / java路径中,另一个声明在src / test / java路径中。

I am trying to override a Spring bean during a test declared in a test configuration with the use of @Primary. One declaration is in the src/main/java path, the other, the primary, is in src/test/java path.

但是,Spring故意替换主要bean与非主bean,我不想用于测试。如果我只是注释掉生产(src / main / java)配置bean,它会根据需要在测试配置中使用主测试(src / main / test)bean。 (显然,每次我想运行测试时都无法注释掉代码。)

However, Spring is intentionally replacing the primary bean with the the non-primary bean, the one I don't want to use for the test. If I simply comment out the production (src/main/java) configuration bean, it uses the primary test (src/main/test) bean in the test configuration as desired. (Clearly I can't comment out code every time I want to run a test.)

从日志中:


osbfsDefaultListableBeanFactory - 使用不同的定义覆盖beansqsConnectionFactory的bean定义:替换 [Root bean:class [null];范围=;抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE; primary = true; factoryBeanName = testJmsConfiguration; factoryMethodName = sqsConnectionFactory; initMethodName = NULL; destroyMethodName =(推断);在类路径资源中定义[com / foo / configuration / TestJmsConfiguration.class]]

o.s.b.f.s.DefaultListableBeanFactory - Overriding bean definition for bean 'sqsConnectionFactory' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=testJmsConfiguration; factoryMethodName=sqsConnectionFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/configuration/TestJmsConfiguration.class]]

with

[Root bean:class [null];范围=;抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE;的初级=假; factoryBeanName = jmsConfiguration; factoryMethodName = sqsConnectionFactory; initMethodName = NULL; destroyMethodName =(推断);在类路径资源中定义[com / foo / configuration / JmsConfiguration.class]]

[Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=jmsConfiguration; factoryMethodName=sqsConnectionFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/configuration/JmsConfiguration.class]]

为什么spring替换了主bean使用非主bean,如何让Spring使用专门标记为主bean的bean?

编辑:
src / main / java配置:

The src/main/java configuration:

@Configuration
public class JmsConfiguration {

... other bean declarations here ...

@Bean
public SQSConnectionFactory sqsConnectionFactory(Region region) throws JMSException {
    return SQSConnectionFactory.builder()
            .withRegion(region)
            .build();
}
}

测试配置:

@Configuration
public class TestJmsConfiguration {

@Bean(name="messageProducerMock")
public MessageProducer mockMessageProducer() {
    return new MessageProducerMock();
}

... other bean declarations here ...

@Bean
@Primary
public SQSConnectionFactory sqsConnectionFactory(@Qualifier("messageProducerMock") MessageProducer messageProducerMock) throws JMSException {
    ... returning setup mock here
}
}

带有测试的类注释为:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(profiles = {"test"})


推荐答案

@Primary 仅在注入点生效,当发生冲突时,因为不同的bean匹配要注入的条件,并且决定需要进行。

@Primary takes effect only at injection point, when there is a conflict because different beans match the condition to be injected, and a decision needs to be made.

@Primary 未在bean初始化时使用。当您使用两种不同的方法创建相同的bean时,并且您没有命名它们中的任何一个Spring认为您正在尝试覆盖它,因此可能会发生此行为。鉴于名称是最简单的解决方案,但请记住,您的上下文仍将初始化您不想使用的bean。

@Primary is not used at beans initialisation. As you are using two different methods creating the same bean, and you are not naming any of them Spring considers you are trying to override it, so this behaviour can happen. Given a name is the easiest solution, but bear in mind that your context will still be initialising the bean you do not want use.

这篇关于Spring重写主bean与非主bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆