【问题标题】:JMS implementation using JNDI in spring application在spring应用程序中使用JNDI实现JMS
【发布时间】:2011-11-25 19:31:18
【问题描述】:

我正在尝试在我的 Spring 应用程序中实现 JMS。我在 applicationContext.xml 中定义了 JNDI 名称 + 队列名称如下:

<bean id="emailQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiName" value="java:comp/env/jms/<<Name of JNDI of connection factory>>" />
</bean>

<bean id="emailQueueDestination" class="org.springframework`enter code here`.jndi.JndiObjectFactoryBean" lazy-init="true">
<property name="jndiName" value="java:comp/env/jms/<<JNDI name of queue>>" />
</bean>

<bean id="emailQueueTemplate" class="org.springframework.jms.core.JmsTemplate" lazy-init="true">
<property name="connectionFactory" ref="emailQueueConnectionFactory" />
<property name="defaultDestination" ref="emailQueueDestination" />
</bean>

<bean id="emailSender" class="<<Package>>.EmailSender" lazy-init="true">
<property name="jmsTemplate">
<ref bean="emailQueueTemplate" />
</property>
</bean>

现在我的控制器使用以下代码调用 emailSender bean:

ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
EmailSender sender =(EmailSender)context.getBean("emailSender");

我得到的异常是:错误 404:请求处理失败;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml];嵌套异常为 java.io.FileNotFoundException: 类路径资源 [applicationContext.xml] 无法打开,因为它不存在

我在 serevr 启动时加载 applicationContext.xml,但我的代码仍然无法找到此文件。

有人可以帮忙吗??

【问题讨论】:

    标签: spring model-view-controller jms jndi


    【解决方案1】:

    确保你的 applicationContext.xml 文件在你的类路径中,然后添加类路径前缀,你可以尝试这样的事情

       ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
    

    【讨论】:

    • 嘿..谢谢你的回复。但是,在我的应用程序中,到目前为止,我们还没有将 applicationContext.xml 包含在类路径中。它与 web.xml 一起加载。所以我担心的是,是否真的有必要将它包含在我的类路径中,因为它是一个非常旧的应用程序,我们是到目前为止,还没有遵循这种做法。
    • 我猜它位于 WEB-INF 文件夹下,如果上下文文件正在 web.xml 中加载(通过加载程序侦听器或调度 servlet),为什么需要再次加载?如果您使用的是 spring 控制器,为什么没有注入 EmailSender?
    • 嘿..我做了同样的事情...因为 applicatoncontext.xml 在服务器启动时被加载...,我只是将我的 bean 注入控制器而不是再次加载 applicationContext。它对我来说非常好......非常感谢您的帮助
    • 嗨,我们在会话中发送的消息是否需要保存。如果我只是生成我的 xml 并将其传递给发送方法,这会导致问题..
    猜你喜欢
    • 2016-06-12
    • 2016-02-07
    • 1970-01-01
    • 2019-05-19
    • 2011-08-10
    • 2012-03-22
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多