【发布时间】:2017-07-22 17:35:34
【问题描述】:
我试图在使用@Primary 的测试配置中声明的测试期间覆盖 Spring bean。一个声明在 src/main/java 路径中,另一个声明主要在 src/test/java 路径中。
但是,Spring 有意将主 bean 替换为非主 bean,我不想将其用于测试。如果我只是注释掉生产 (src/main/java) 配置 bean,它会根据需要使用测试配置中的主要测试 (src/main/test) bean。 (显然我不能每次想运行测试时都注释掉代码。)
来自日志:
o.s.b.f.s.DefaultListableBeanFactory - 用不同的定义覆盖 bean 'sqsConnectionFactory' 的 bean 定义:替换 [Root bean: class [null];范围=;摘要=假;懒惰初始化=假;自动线模式=3;依赖检查=0;自动接线候选=真; primary=true; factoryBeanName=testJmsConfiguration; factoryMethodName=sqsConnectionFactory;初始化方法名=空; destroyMethodName=(推断);在类路径资源中定义 [com/foo/configuration/TestJmsConfiguration.class]]
与
[根bean:类[null];范围=;摘要=假;懒惰初始化=假;自动线模式=3;依赖检查=0;自动接线候选=真; primary=false; factoryBeanName=jmsConfiguration; factoryMethodName=sqsConnectionFactory;初始化方法名=空; destroyMethodName=(推断);在类路径资源中定义 [com/foo/configuration/JmsConfiguration.class]]
为什么 spring 要用非主 bean 替换主 bean,我如何让 Spring 使用专门标记为主 bean 的 bean?
编辑: src/main/java 配置:
@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"})
【问题讨论】:
-
请添加测试配置和有问题的测试类。
-
两种不同的方法创建一个同名的bean,这样就可以发生这种行为。为您的主 bean 命名:@Bean(name="nameforbean")
-
@alfcope 这就是答案,谢谢!如果您将此作为答案提交,我将接受它为正确的。我不知道当 Spring 知道他们正在用未标记为主要的 bean 替换标记为主要的 bean 时,为什么 Spring 会允许这种行为。
-
我很高兴它可以工作@FiguringThisOut。你有一个带有一点解释的答案。
标签: java spring spring-boot dependency-injection spring-test