You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Overview

Error formatting macro: snippet: java.lang.NullPointerException

The Code

In this example we develop two simple session stateless beans (DataReader and DataStore), and show how we can use the @EJB annotation in one of these beans to get the reference to the other session bean

DataStore session bean

Local business interface
Error formatting macro: snippet: java.lang.NullPointerException
Remote business interface
Error formatting macro: snippet: java.lang.NullPointerException
Bean
Error formatting macro: snippet: java.lang.NullPointerException

DataReader session bean

Local business interface
Error formatting macro: snippet: java.lang.NullPointerException
Bean
Error formatting macro: snippet: java.lang.NullPointerException

(The remote business interface is not shown for the sake of brevity).

@EJB annotation

You can use the @EJB annotation to refer a session bean in other components specified by the Java EE platform, for instance:

  • Servlets
  • ServletContextListeners
  • Servlet Filters
  • JSF managed beans
  • EJB interceptors
  • JAX-WS service endpoints

Writing a unit test for the example

Writing an unit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods

setUp

Error formatting macro: snippet: java.lang.NullPointerException

Test

Error formatting macro: snippet: java.lang.NullPointerException

Running

Running the example is fairly simple, just run:

$ cd ejb-injection
$ mvn clean install

Which should create output like the following.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
[OPENEJB:surefire] Running org.apache.openejb.examples.injection.EjbDependencyTest
log4j:WARN No appenders could be found for logger (OpenEJB).
log4j:WARN Please initialize the log4j system properly.
Apache OpenEJB 3.0-incubating-SNAPSHOT    build: 20070105-12:45
http://incubator.apache.org/openejb
OpenEJB ready.
[OPENEJB:surefire] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 2.006 sec
[OPENEJB:INFO] [jar:jar]
[OPENEJB:INFO] Building jar: openejb3/examples/ejb-injection/target/ejb-injection-sample-1.0-SNAPSHOT.jar
[OPENEJB:INFO] [install:install]
[OPENEJB:INFO] Installing openejb3/examples/ejb-injection/target/ejb-injection-sample-1.0-SNAPSHOT.jar to ...
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] BUILD SUCCESSFUL
[OPENEJB:INFO] ------------------------------------------------------------------------

  • No labels