Spring Rabbit Queue Xml:从'Getter Bean'获取属性时的NoSuchBeanDefinition

时间:2017-02-05 01:42:01

标签: java spring spring-rabbit xmlbeans

我正在使用xml文件为Spring Rabbit bean和配置配置Spring Rabbit。

我们使用自己的propertyGetter类来检索属性。在下面的春天兔子xml中,它除了一个地方外无处不在。这是为什么?

下面是xml。被调用的'propertyGetter.get(...)'方法返回一个String'my.queue',但似乎RabbitMq xsd解析器正试图获取一个bean。 (?)。

<?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:rabbit="http://www.springframework.org/schema/rabbit"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">

        <!-- This has the 'propertyGetter' bean -->
        <import resource="**/applicationContext.xml"/>

        <!--  RabbitMQ -->
        <rabbit:connection-factory id="rabbitConnectionFactory" addresses="#{propertyGetter.get('rabbitMq.hostName')}" username="#{propertyGetter.get('rabbitMq.userName')}" password="#{propertyGetter.get('rabbitMq.password')}" virtual-host="#{propertyGetter.get('rabbitMq.vHost')}"/>
        <rabbit:admin connection-factory="rabbitConnectionFactory"/>

        <!-- RabbitMQ Template -->
        <rabbit:template id="rabbitTemplate" connection-factory="rabbitConnectionFactory" message-converter="messageConverter" exchange="#{propertyGetter.get('rabbitMq.exchange')}"/>


        /*******************************************
         *
         *    THIS IS THE ONLY PLACE IT DOESN'T WORK
         *    <rabbit:queue name=""
         *        It does seem to get value 'my.queue'
         *        but the exception that's posted
         *        below indicates it's trying to 
         *        construct a bean!  Why is this?
        *******************************************/
        <!-- RabbitMQ Queue Definitions -->
        <!--<rabbit:queue name="my.queue" >--> <!--THIS WORKS-->
        <rabbit:queue name="#{propertyGetter.get('rabbitMq.queue.name')}" > <!--THIS DOESN'T-->
            <rabbit:queue-arguments>
                <entry key="x-ha-policy" value="all"/>
            </rabbit:queue-arguments>
        </rabbit:queue>

        <!-- RabbitMQ Exchange and Binding Definition -->
        <rabbit:direct-exchange name="#{propertyGetter.get('rabbitMq.exchange')}">
            <rabbit:bindings>
                <rabbit:binding key="#{propertyGetter.get('rabbitMq.routing.key')}" queue="#{propertyGetter.get('rabbitMq.queue.name')}"/>
            </rabbit:bindings>
        </rabbit:direct-exchange>

        <!-- Listeners -->
        <rabbit:listener-container id="listener" connection-factory="rabbitConnectionFactory" concurrency="3" max-concurrency="5" requeue-rejected="false">
            <rabbit:listener ref="eventNotificationAdapter" queues="#{propertyGetter.get('rabbitMq.queue.name')}" priority="#{propertyGetter.get('rabbitMq.queue.priority')}" />
        </rabbit:listener-container>

        <!-- Message Listener Adapters -->
        <bean id="eventNotificationAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
            <constructor-arg name="delegate" ref="myQueueConsumer"/>
            <constructor-arg name="messageConverter" ref="messageConverter"/>
            <property name="defaultListenerMethod" value="process"/>
        </bean>

        <bean id="messageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter"/>

</beans>

首次初始化应用程序时的异常是“NoSuchBeanDefinitionException”,但我不确定解析xml的原因是什么,当方法调用返回一个String时,它正在期待一个bean。

这是我在尝试部署应用时获得的堆栈跟踪:

SEVERE: Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.spring.SpringContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.config.BindingFactoryBean#0': Cannot resolve reference to bean '#{propertyGetter.get('rabbitMq.queue.name')}' while setting bean property 'destinationQueue'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'my.queue' is defined
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.jboss.resteasy.plugins.spring.SpringContextLoaderListener.contextInitialized(SpringContextLoaderListener.java:48)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5017)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5531)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'my.queue' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 29 more 

其他说明: 这是一个使用spring-rabbit-1.3.xsd

的xml文件

该解决方案不使用弹簧属性。这是用于以另一种方式管理属性。

1 个答案:

答案 0 :(得分:1)

它们是两回事。

对于<bean/>name属性是bean名称的别名。如果未提供id,则会将第一个name用作ID。

对于<queue/>name属性是rabbitmq代理中队列的名称; id始终是bean名称,如果不存在,则生成唯一的bean名称。

有一些现有逻辑可以将name注册为别名,但只有在分析时知道该值时才会有效(如您所见)。

事实上,这种别名的使用不正确并导致其他问题 - 例如,您无法使用相同的name进行交换和排队 - 请参阅this JIRA for a discussion about that

要解决这个问题,我们正在考虑完全删除别名并要求id属性为bean命名。

我相信您可以通过给队列id并在您的侦听器容器queues属性中引用该队列来解决您的问题,或者将容器更改为使用queue-names