Quantcast
Channel: Java EE Dependency Injection in Websphere Liberty profile - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Java EE Dependency Injection in Websphere Liberty profile

$
0
0

I am trying to use CDI in my very simple web app that runs in Websphere Liberty profile installed via Docker.

However the injection fails unless I specify a scope annotation (e.g. @ApplicationScoped) on the injected bean, though according to a lot of online tutorials (e.g. this), Java EE specs do not require this.

Below is the code that fails:

HelloWorldServlet.java

package my.simple.app;import javax.inject.Inject;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;@WebServlet("/HelloWorld")public class HelloWorldServlet extends HttpServlet {    static String PAGE_HEADER = "<html><head /><body>";    static String PAGE_FOOTER = "</body></html>";    @Inject    HelloService helloService;    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        PrintWriter writer = resp.getWriter();        writer.println(PAGE_HEADER);        writer.println("<h1>"+ helloService.createHelloMessage("World") +"</h1>");        writer.println(PAGE_FOOTER);        writer.close();    }}

HelloService.java

package my.simple.app;public class HelloService {    String createHelloMessage(String name) {        return "Hello "+ name +"!";    }}

server.xml (Docker image is websphere-liberty:javaee7)

<server description="default servlet engine"><httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9080" httpsPort="9443" /><!-- Enable features --><featureManager><feature>servlet-3.1</feature><feature>cdi-1.2</feature></featureManager></server>

However I get this error

Error 404: javax.servlet.UnavailableException: SRVE0319E: For the [my.simple.app.HelloWorldServlet] servlet, my.simple.app.HelloWorldServlet servlet class was found, but a resource injection failure has occurred. The @Inject java.lang.reflect.Field.helloService reference of type my.simple.app.HelloService for the null component in the app.war module of the app application cannot be resolved.

However once I add @ApplicationScoped to HelloService it all starts working.

What I am doing wrong?

Solution:

In CDI1.2 (which I am using) by default only annotated beans are discovered. To make all beans be discovered, and explicit discovery mode needs to be enabled in beans.xml

Links:


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images