Tuesday, November 18, 2008

When Is Vitamin K Bad For You

Injecting dependencies in a Web application

You know that feeling when you're doing something after the umpteenth time, and each time worked and now nothing in the world does not want? Exactly ... I needed to do a little test to create EJB3 project in it a SLSB. Apart from this I created a web application project (Dynamic Web Project), in the middle of a class implementing ServletContextListener to be notified when injected Bean deployu EAR'a tudziez start the application server:
 
public class implements MyStartupListener ServletContextListener {

@ EJB private
LifecycleMonitorRemote lifeCycleMonitor

public void contextDestroyed (final ServletContextEvent arg0) {
lifeCycleMonitor.shutdown ();}


public void contextInitialized (final ServletContextEvent arg0) {
lifeCycleMonitor.startup ();}

}

I added the appropriate entries to the web.xml:
 
\u0026lt;? xml version = "1.0" encoding = "UTF-8"?>
\u0026lt; web-app id = "WebApp_ID" version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun .com/xml/ns/j2ee/web-app_2_4.xsd ">
\u0026lt;display-name> test.startup.web \u0026lt;/ display-name>
\u0026lt;welcome-file-list>
\u0026lt;welcome-file> ; index.html \u0026lt;/ welcome-file>
\u0026lt;welcome-file> index.htm \u0026lt;/ welcome-file>
\u0026lt;welcome-file> index.jsp \u0026lt;/ welcome-file> default \u0026lt;welcome-file>
. html \u0026lt;/ welcome-file>
\u0026lt;welcome-file> default.htm \u0026lt;/ welcome-file>
\u0026lt;welcome-file> default.jsp \u0026lt;/ welcome-file>
\u0026lt;/ welcome-file-list>

\u0026lt;listener>
\u0026lt; ; Listener-class> listeners.MyStartupListener \u0026lt;/ Listener-class>
\u0026lt;/ Listener>

\u0026lt;servlet>
\u0026lt;description> \u0026lt;/ description>
\u0026lt;display-name> MyStartupListener \u0026lt;/ display-name>
\u0026lt;servlet-name> MyStartupListener \u0026lt;/ servlet-name>
\u0026lt;servlet-class> listeners.MyStartupListener \u0026lt;/ servlet-class>
\u0026lt;/ servlet>

\u0026lt;servlet-mapping>
\u0026lt;servlet-name> MyStartupListener \u0026lt;/ servlet-name>
\u0026lt;url-pattern> ; / MyStartupListener \u0026lt;/ url-pattern>
\u0026lt;/ servlet-mapping>
\u0026lt;/ web-app>
we go ... And what? I NullPointerException in line
 lifeCycleMonitor.startup (); 
means dependency injection if it did not work ... But it's always worked ...
several times to review all the code and nothing (!) In such situations usually helps Google - I found the clue JavaRanch , and the error was more prosaic than I thought. As you can see the web.xml file above refers to the Java Servlet specification version 2.4, while the dependency injection in servlets running on version 2.5. After the amendment, this piece web.xml looks like this:
 
\u0026lt;web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun. com / xml / ns / JavaEE "
xmlns: web =" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
xsi: schemaLocation =" http://java.sun. com / xml / ns / JavaEE http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
id =" WebApp_ID "version =" 2.5 ">

Phew ...

0 comments:

Post a Comment