1

I am a very newbie in CDI. This is my FIRST example and I am trying to run it. Having searched the internet I wrote the following code: Class that I want to be injected

public class Temp {

public Temp(){

}

public String getMe(){
    return "something";
}
}

Servlet

@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {

@Inject
public Temp temp;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<body>");
        out.println("<h1> Here it is"+temp.getMe()+ "</h1>");
        out.println("</body>");
    }
}
...

But I have to following error in glassfish 4:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Temp] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private xxx.example.NewServlet.temp]

What am I doing wrong?

2 Answers2

11

Either no beans.xml exists within WEB-INF or the file requires changing bean-discovery-mode="annotated" to bean-discovery-mode="all".


<?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>

Explanation

The recommended value "annotated" only recognizes annotated CDI managed beans. Beans without any annotation are ignored. As your Temp class is not CDI bean, so recommendation is not applicable in your case.

Using bean-discovery-mode="annotated"

To work with annotated, annotate the class with @RequestScoped:

// Import only this RequestScoped
import javax.enterprise.context.RequestScoped;

@RequestScoped
public class Temp {

    public Temp() { }

    public String getMe() {
        return "something";
    }
}

This RequestScoped will convert your Temp class to CDI bean and will be work with bean-discovery-mode="annotated".

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Masudul
  • 21,823
  • 5
  • 43
  • 58
3

At me worked with this command:

asadmin set configs.config.server-config.cdi-service.enable-implicit-cdi=false

So disable the enable-implicit-cdi worked for me.

mariobyn
  • 86
  • 7
  • Ok, so this is from terminal ? – mjs Aug 15 '16 at 18:19
  • Or can this be passed as an argument? – mjs Aug 15 '16 at 18:19
  • When I try this, I get "NCLS-ADMIN-00010 javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateExpiredException: NotAfter: Tue Apr 03 18:09:20 EDT 2018 Command set failed." But I have checked all my certs in cacerts.jks and keystore.jks, and none of them are expired. Help? – Mark J. Bobak May 14 '19 at 17:15