无法实例化CDI bean

时间:2016-06-17 12:41:53

标签: java-ee cdi

无法从simpe cdi webapp实例化一个bean。

的servlet

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

private static final long serialVersionUID = 2280890757609124481L;

    @Inject
    @Greetings(GreetingType.HELLO)
    private GreetingCard greetingCard;

      public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("<h1>" + greetingCard.sayHello() + "</h1>");
      }

}

pom.xml依赖项

<dependencies>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.10.Final</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>    
<version>3.0.1</version>    
<scope>provided</scope>    
</dependency>    

<dependency>    
<groupId>org.glassfish</groupId>    
<artifactId>javax.faces</artifactId>    
<version>2.1.7</version>    
</dependency> 
</dependencies>

我收到错误:

SEVERE: Servlet.service() for servlet [greetingServlet] in context with path [/CDI_prog] threw exception
java.lang.NullPointerException
    at com.javacodegeeks.snippets.enterprise.cdibeans.servlet.GreetingServlet.doGet(GreetingServlet.java:37) //`greetingCard.sayHello()`
...

Eclipse项目中没有错误。我认为依赖的问题让Tomcat无法检测到CDI。是否遗漏了一些依赖?

1 个答案:

答案 0 :(得分:0)

Tomcat 7不支持CDI。您应该使用JavaEE 6应用程序容器,如Glassfish,Wildfly或其他东西。依赖关系中你有什么关系并不重要。