简体   繁体   中英

CDI in a servlet produces NullPointerException

I am trying to use CDI in a servlet, here is a snippet of my servlet code

public class MyServlet extends HTTPServlet{

  @Inject
  @Any
  Instance<MyProcedure> procedures;

  public void handleRequest(ServletRequest req, ServletResponse res) throws Exception {

    if (procedures == null) {
        System.out.println("procedure list is NULL");
    }
    for (Object o : procedures) {
        System.out.println("calling procedure " + o.toString());
    }
  }
}

MyProcedure is am interface and the appliation WAR contains a couple of classes that implement MyProcedure.

I always get a NullPointerException because the list of procedures is null...I do have a beans.xml file in my WEB-INF directory.

I found out what the problem was. The interface that was implemented by my beans had to be included in the war archive under WEB-INF/lib. After I did that everything started to work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM