cdi - Java EE Dependency Injection in Websphere Liberty profile -


i trying use cdi in simple web app runs in websphere liberty profile installed via docker.

however injection fails unless specify scope annotation (e.g. @applicationscoped) on injected bean, though according lot of online tutorials (e.g. this), java ee specs not require this.

below code 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 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 error

error 404: javax.servlet.unavailableexception: srve0319e: [my.simple.app.helloworldservlet] servlet, my.simple.app.helloworldservlet servlet class found, resource injection failure has occurred. @inject java.lang.reflect.field.helloservice reference of type my.simple.app.helloservice null component in app.war module of app application cannot resolved. 

however once add @applicationscoped helloservice starts working.

what doing wrong?

solution:

in cdi1.2 (which using) default annotated beans discovered. make beans discovered, , explicit discovery mode needs enabled in beans.xml

links:

you can force cdi treat servlet bean , perform injection changing bean discovery mode all.

this article provides useful background , example of this:

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"    xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee     http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"    bean-discovery-mode="all"> </beans> 

or, in wdt, can generate right-clicking project , selecting java ee tools -> generate cdi beans deployment descriptor stub, , sure select all drop-down "bean discovery mode" selection.

the downside performance hit since app take longer start up, that's tradeoff make avoid recompiling.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -