有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring 3.0 lazyinit不接受DefaultMessageListenerContainer?

我已经为JMS设置了一个spring配置。一切正常,只是我似乎无法将其加载到延迟加载(请注意下面代码中默认的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。爪哇:

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名称,这样我就可以看到事情何时开始


共 (2) 个答案

  1. # 1 楼答案

    解决方案是使用AutoStart to false。请参阅下面的代码

    <bean id="listenerContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
         ........
        <property name="autoStartup" value="false"/>
    </bean>
    

    ~Shyam

  2. # 2 楼答案

    好的,这是相当模糊的,但是DefaultMessageListenerContainer实现了Lifecycle接口,实现这个接口的bean被绑定到上下文自己的生命周期中——当上下文启动时,Lifecycle——实现bean被初始化并启动。这意味着您的lazy init配置基本上被忽略