繁体   English   中英

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

[英]Spring Bean Creation Exception when Conflicts in Beans

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

@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)

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

您声明了名为bean的exchange例如:

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

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

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

正如@ConditionalOnMissingBean javadoc指出的(强调是我的):

仅当指定的Bean类和/或名称尚未包含在BeanFactory中时, 条件才匹配。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM