java - Spring 3.0 lazy-init 不支持 DefaultMessageListenerContainer?

标签 java spring jms lazy-initialization

我已经为 JMS 设置了一个 spring 配置。一切正常,除了我似乎无法让它延迟加载(注意下面代码中的 default-lazy-init true)。如果我从下面的配置中注释掉 jmsContainer(DMLC),延迟加载将按预期工作。否则,它将实例化 DMLC,后者又会创建队列和连接工厂。

我错过了什么?

jmsContext.xml:

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-lazy-init="true">

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
                <prop key="java.naming.provider.url">t3:localhost:7001</prop>
            </props>
        </property>
    </bean>

    <bean id="queue" class="org.springframework.jndi.JndiObjectFactoryBean"
          p:jndiTemplate-ref="jndiTemplate" p:jndiName="jms/queue"/>

    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"
          p:jndiTemplate-ref="jndiTemplate" p:jndiName="jms/connectionfactory"/>

    <bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver"
        p:jndiTemplate-ref="jndiTemplate" p:cache="true" />

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"
          p:autoStartup="false"
          p:destination-ref="queue"
          p:destinationResolver-ref="jmsDestinationResolver"
          p:connectionFactory-ref="connectionFactory"
          p:messageListener-ref="queueListener" />

    <bean id="queueListener" class="com.blah.QueueListener"/>


</beans>

我用来驱动它的测试,DummyTest.java:

package blah;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:jmsContext.xml")
public class DummyTest {

    @Test
    public void shouldDoSomething() {

    }

}

当 jmsContainer 被注释掉时,上面的测试通过。否则,我得到这个:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'jmsContainer' defined in class path resource [com/blah/config/jmsContext.xml]: 
Cannot resolve reference to bean 'connectionFactory' while setting bean property 'connectionFactory'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'connectionFactory' defined in class path resource [com/blah/config/jmsContext.xml]: 
Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: 
Exception in lookup.: `jms/connectionfactory' could not be found. 
[Root exception is weblogic.corba.cos.naming.NamingContextAnyPackage.NotFound: IDL:weblogic/corba/cos/naming/NamingContextAny/NotFound:1.0]

“connectionFactory”bean 被实例化为“jmsContainer”的依赖项,但它失败了。注释掉“jmsContainer”后,“connectionFactory”不会被实例化。

jms 代码工作正常,但我特意重命名了我的 JNDI 名称,以便我可以看到事情何时开始。

最佳答案

好吧,这很晦涩,但是 DefaultMessageListenerContainer 实现了 Lifecycle 接口(interface),实现它的 bean 被绑定(bind)到上下文自己的生命周期中 - 当上下文启动时, Lifecycle - 实现 bean 已初始化并启动。这意味着您的 lazy-init 配置基本上被忽略了。

关于java - Spring 3.0 lazy-init 不支持 DefaultMessageListenerContainer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4412628/

相关文章:

java - 使用 PHP 在网页中使用 Lucene

java - JPQL:在构造函数表达式中接收集合

java - 使用 Jackson 和 Spring Boot 的条件 JsonProperty

java - 用于 Tomcat 6 的 JMS

java - 清除 jBoss DLQ

java - 将 Java.Util.Date 转换为 Java.Sql.Date 时出错

java - Jhipster UAA 外部客户端

java - 在没有 <mvc :resources/> tag 的情况下在 Freemarker 中使用 CSS 样式文件

java - Spring 不使用抽象父类(super class)将 DAO 注入(inject)到 JSF 托管 bean 中

JMS 和 ActiveMQ 异常