Bean中发生冲突时,Spring Bean创建异常

时间:2018-07-12 13:57:18

标签: java spring spring-boot rabbitmq

以下是我们项目中使用的普通启动器中的代码。

@ConditionalOnProperty(prefix = "some.prefix", value = "some-enable")
@EnableConfigurationProperties(value = {
    SomeProperties.class,
})
@Configuration
class RabbitGenericValueConfiguration {

    @Autowired
    public void setuoRabbit(AmqpAdmin admin) {
        admin.declareExchange(exchange());
    }

    @ConditionalOnProperty(prefix = "existing.property", value = "setup")
    @ConditionalOnMissingBean
    @Bean
    Exchange exchange() {
        return ExchangeBuilder.topicExchange(properties.getExchange())
            .build();
    }

}    

在我们的服务中,我们需要创建一个自定义交换,但是,每当添加以下代码时,就会发生以下错误

@Configuration
    public class CustomConfiguration {


        @Bean
        public DirectExchange direct() {
            return new DirectExchange("test.direct");
        }

    }

例外是

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'exchange' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.obtainBeanInstanceFromFactory(ConfigurationClassEnhancer.java:389)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
    at com.something.RabbitGenericValueConfiguration$$EnhancerBySpringCGLIB$$2df2c1f1.exchange(<generated>)
    at com.something.RabbitGenericValueConfiguration.setuoRabbit(RabbitGenericValueConfiguration.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

问题是为什么在没有自定义交换的情况下启动应用程序,而在有自定义交换的情况下为何失败?

1 个答案:

答案 0 :(得分:0)

您声明了exchange命名的bean,例如:

@ConditionalOnProperty(prefix = "existing.property", value = "setup")
@ConditionalOnMissingBean
@Bean
Exchange exchange() {
    return ExchangeBuilder.topicExchange(properties.getExchange())
        .build();
}

听起来两个声明的bean都来自Exchange类。因此,@ConditionalOnMissingBean批注的存在会阻止如果先实例化该bean则实例化该bean:

@Bean
public DirectExchange direct() {
    return new DirectExchange("test.direct");
}

正如@ConditionalOnMissingBean javadoc所说(强调是我的):

  

仅在指定的Bean类和/或   名称尚未包含在BeanFactory中。

如果希望Spring实例化两个实例,则应该删除@ConditionalOnMissingBean