How to install and use CDI on Tomcat?

后端 未结 2 654
情歌与酒
情歌与酒 2020-11-22 03:31

I\'m creating my first project Java EE 7, but I\'m having trouble. Appreciate any help.

  • Tomcat 7.0.34
  • JSF 2.2
  • Primefaces 3.5
  • javaee-
相关标签:
2条回答
  • 2020-11-22 04:03

    Tomcat as being a barebones JSP/Servlet container doesn't support CDI out the box. How exactly did you install CDI? Did you really drop javaee-api.jar in /WEB-INF/lib just to get your code to compile? Oh please no, this is not the right way. The Java EE API JAR contains solely the API classes, not the concrete implementation. Get rid of the whole JAR. It can cause many other portability troubles like as the ones described in this answer: How do I import the javax.servlet API in my Eclipse project? You should actually be installing the concrete implementation along with the specific API.

    You have 2 options:

    1. Drop Tomcat and go for a true Java EE container. As you're using Tomcat, just step over to TomEE. It's really simple, download the TomEE web profile zip file, extract it and integrate it in Eclipse exactly the same way as you did for Tomcat. Don't forget to remove the Java EE JAR file from webapp and alter the Targeted Runtime property in project's properties from Tomcat to TomEE so that Java EE dependencies are properly resolved.

      No additional JARs or configuration is necessary. You can even remove the JSF JARs from your webapp. TomEE as being a true Java EE container already provides among others JSF and CDI out the box. In case you're using Maven, the below coordinate is sufficient.

       <dependency>
           <groupId>javax</groupId>
           <artifactId>javaee-web-api</artifactId>
           <version><!-- 8.0 or 7.0 or 6.0 --></version>
           <scope>provided</scope>
       </dependency>
      

      Note the importance of provided and its meaning as in "the target runtime already provides this out the box". See also How to properly install and configure JSF libraries via Maven?.


    2. Install a true CDI implementation on Tomcat. Weld is one of the available CDI implementations. In the Weld installation guide you can find instructions how to integrate it in Tomcat. For sake of completeness and future reference, here are the steps:

      1. Drop the weld-servlet-shaded.jar in webapp's /WEB-INF/lib. In case you're using Maven, use this coordinate:

         <dependency>
             <groupId>org.jboss.weld.servlet</groupId>
             <artifactId>weld-servlet-shaded</artifactId>
             <version>3.1.5.Final</version>
         </dependency>
        
      2. Create /META-INF/context.xml file in webapp with following content:

         <Context>
             <Resource name="BeanManager" 
                 auth="Container"
                 type="javax.enterprise.inject.spi.BeanManager"
                 factory="org.jboss.weld.resources.ManagerObjectFactory"/>
         </Context>
        

        Note that this step is not strictly necessary when you're using Mojarra 2.2.11 or newer as it will be able to find it via ServletContext when absent in JNDI.

      3. Create a /WEB-INF/beans.xml file in webapp. It can be kept empty.

      4. Only if your web.xml is declared conform Servlet version 4.0 instead of 3.1, then you also need to put the @javax.faces.annotation.FacesConfig annotation on an arbitrary CDI managed bean somewhere in the project (usually the one representing the "application-wide config" would be OK).

         package com.example;
        
         import javax.enterprise.context.ApplicationScoped;
         import javax.faces.annotation.FacesConfig;
        
         @FacesConfig
         @ApplicationScoped
         public class Config {
             // ...
         }
        

        It is indeed utterly unnecessary, but it is what it is.

      That's it (note: in older Weld Servlet versions, you'd need to explicitly register the CDI bean manager and Weld listener in web.xml too, but that's unnecessary with current versions).

      In case you prefer OpenWebBeans above Weld as CDI implementation, head to this blog for detailed Maven installation instructions: How to install CDI in Tomcat?


    Unrelated to the concrete problem, the JSP/Servlet APIs of Tomcat 7 do not comply those APIs of Java EE 7, instead it complies Java EE 6 (Servlet 3.0 / JSP 2.2). If you want the Tomcat equivalent of Java EE 7 (Servlet 3.1 / JSP 2.3), then you should be looking at Tomcat 8. See also Apache Tomcat version matrix.

    0 讨论(0)
  • 2020-11-22 04:06

    Other possible option is leaving beans.xml in your deployment.

    0 讨论(0)
提交回复
热议问题