【问题标题】:Problems with resource injection in EJBsEJB 中的资源注入问题
【发布时间】:2012-05-19 01:47:21
【问题描述】:

我无法使用注释资源获取我的连接工厂。 它适用于 JNDI 查找,但不适用于注释,我的连接工厂为空。

这是我的 JNDI 查找代码(有效):

ConnectionFactory factory = (ConnectionFactory)context.lookup("/ConnectionFactory");

这是我带有注释的代码:

@Resource(mappedName = "java:/ConnectionFactory")
private ConnectionFactory factory;

我尝试过使用不同的 mappedName,如:/ConnectionFactory、java:/JmsXA、JmsXA 等,但仍然是 nullpointerexception:-/。

如果有人有想法...

谢谢!

这是我的课:

@Stateless
public class ModuleCommunicationHandler implements IModuleCommunicationHandler
{

  /** The connection factory. */
  @Resource(mappedName = "java:/ConnectionFactory")
  private ConnectionFactory factory;

...........

/**
* {@inheritDoc}
*/
@Override
public void sendMessage(JMSMessage jmsMessage, int deliveryMode, int acknowledgeMode) throws TechnicalException
{

try
{
  context = new InitialContext();

  factory = (ConnectionFactory)context.lookup("/ConnectionFactory");

  // Setting the destination - Topic or Queue
  destination = (Destination)context.lookup(jmsMessage.getDestination());

  connection = factory.createConnection();
  session = connection.createSession(false, acknowledgeMode);

  sender = session.createProducer(destination);
  connection.start();

  // Creating the message
  message = session.createTextMessage();
  message.setText(jmsMessage.getBodyMessage());

  // Sending the message
  sender.setDeliveryMode(deliveryMode);
  sender.send(message);

.....

这是日志:

15:05:32,609 ERROR [STDERR] com.*.*.*.server.exception.ModuleCommunicationException: java.lang.NullPointerException
15:05:32,609 ERROR [STDERR]     at com.*.*.*.server.service.ModuleCommunicationHandler.sendMessage(ModuleCommunicationHandler.java:147)
15:05:32,664 ERROR [STDERR] Caused by: java.lang.NullPointerException
15:05:32,664 ERROR [STDERR]     at com.*.*.*.server.service.ModuleCommunicationHandler.sendMessage(ModuleCommunicationHandler.java:108)

【问题讨论】:

    标签: java jboss ejb jms


    【解决方案1】:

    如果没有看到你所有的代码(对于有问题的类),很难说,但这里有一些常见的嫌疑人:

    1. @Resource 注解实例没有指定类型,所以会默认为字段的类型。如果实际资源是 a.b.c.ConnectionFactory,但您的字段是 x.y.z.ConnectionFactory,则会出现某种错误。
    2. 您的注释类可能未部署为托管(注入)类。换句话说,如果您不确保在部署该类时将其视为 EJB,则注入注释将不会被接受。见this post for an example

    发生这种情况时,您的日志中没有任何堆栈跟踪吗?

    【讨论】:

    • 尝试与我的工厂建立连接时抛出错误
    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多