EntityManager injection results in NullPointerException

一个人想着一个人 提交于 2019-12-04 14:48:00
Pascal Thivent

Quoting Referencing EJB3 Session Beans from non-EJB3 Beans from the JBoss documentation:

JBoss Application Server 4.2.2 implemented EJB3 functionality by way of an EJB MBean container running as a plugin in the JBoss Application Server. This had certain implications for application development.

The EJB3 plugin injects references to an EntityManager and @EJB references from one EJB object to another. However this support is limited to the EJB3 MBean and the JAR files it manages. Any JAR files which are loaded from a WAR (such as Servlets, JSF backing beans, and so forth) do not undergo this processing. The Java 5 Enterprise Edition standard specifies that a Servlet can reference a Session Bean through an @EJB annotated reference, this was not implemented in JBoss Application Server 4.2.2.

So even though the Java EE 5 specification stipulates a wider scope of the @EJB annotation, JBoss 4.2.2 doesn't support it. In fact JBoss 4.2.2 is a transitional version that doesn't claim full Java EE 5 compliance.

Consequently, either:

  • Stick with you actual version of JBoss but use a JDNI lookup to get your bean - or -
  • Swith to JBoss 5 AS that fully supports the entire Java 5 Enterprise Edition specification (but has horrible startup performances) - or -
  • Swith to another Java EE 5 application server like GlassFish v2 (or even v3) or WebLogic 10. I'd go with GFv3.

As Petar Minchev said in the comments above @EJB doesn't work in servlets with JBoss 4.2.

In your WAR (in the WEB-INF directory) you'll need to modify two files:

web.xml:

<ejb-local-ref>
        <ejb-ref-name>ejb/UserManager</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local-home/>
        <local>myPackage.UserManager</local>
</ejb-local-ref> 

jboss-web.xml:

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>

  <ejb-local-ref>
    <ejb-ref-name>ejb/UserManager</ejb-ref-name>
    <local-jndi-name>[earname]/UserManagerBean/local</local-jndi-name>
  </ejb-local-ref>

</jboss-web>

You can then get an instance of your EJB by doing:

UserManagerBean um = (UserManager)new InitialContext().lookup( "java:comp/env/ejb/UserManager" );

As per the comments, it turns out that you indeed are using an appserver which doesn't support it at all -as I expected. To resolve this, you have 3 options:

  1. Use "good old" XML config files.
  2. Upgrade the server (there's already a stable 5.1.0).
  3. Replace the server (I can recommend Glassfish v3 to be the most up-to-date).

To enable dependency injection on your project make sure that you have the file "beans.xml" and it is properly located.

  • For WAR packaging the location is src/main/webapp/WEB-INF/
  • For JAR and EAR packaging the location is src/main/resources/META-INF/

If you have several maven modules you must have it on each module.

More information about beans.xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!