注入JMS @Resource时,Glassfish中的NullPointer [英] NullPointer in Glassfish when inject JMS @Resource

查看:73
本文介绍了注入JMS @Resource时,Glassfish中的NullPointer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在类中发送消息我有以下几点:$ b​​
$ b

  @Resource(mappedName =jms / MyConnectionFactory)
public static QueueConnectionFactory factory;

@Resource(mappedName =jms / MyQueue)
公共静态队列队列;

这导致了一个NullPointer,我第一次尝试访问工厂。如果我尝试使用JNDI查找对象,它就会起作用。这个类只是一个简单的Web应用程序中运行的JSP访问的POJO。



是因为它是POJO而不是,例如,这是失败的servlet?



我曾假设它在Glassfish内部署和运行的事实就足够了 - 显然我错了......

name 而不是 mappedName name 是JNDI名称, mappedName 是别的。请参阅 API文档

  @Resource(name =jms / MyConnectionFactory)
public static QueueConnectionFactory factory;

我刚刚读到你的课是一个简单的POJO。您的类必须是某个由容器管理的bean,才能使这些注释生效。像@Stateless,@Stateful,@Singleton一样。

I'm trying to write a very simple JMS app to deploy on Glassfish as a means of getting my head around JMS.

In the class the sends the message I have the following:

    @Resource(mappedName="jms/MyConnectionFactory")
    public static QueueConnectionFactory factory;

    @Resource(mappedName="jms/MyQueue")
    public static Queue queue;

This results in a NullPointer, the first time I try and access the factory. If I try and look up the object using JNDI, however, it works. This class is simply a POJO which is accessed by a JSP running in a simple web app.

Is it because it is a POJO and not, for example, a servlet that this is failing?

I had assumed that the fact it was deployed and running inside Glassfish would be enough - obviously I am wrong...

解决方案

Use name instead of mappedName. name is the JNDI name, mappedName is something else. See the API doc

@Resource(name="jms/MyConnectionFactory")
public static QueueConnectionFactory factory;

I just read that your class is a simple POJO. Your class must be some bean that is managed by a container in order to make these annotations work. Like @Stateless, @Stateful, @Singleton.

这篇关于注入JMS @Resource时,Glassfish中的NullPointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆