Model is not injected into servlet

I have a simple servlet project: I need to inject a dependency to a servlet instance. I've tried several annotations: Default , Singlton , EJB , ManagedBean , nothing helps.

POM.xml part

<build>
    <resources>
        <resource>
          <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>

                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

      

servlets

@WebServlet(urlPatterns = {"/updater_servlet"},loadOnStartup = 1)
public class Updater_servlet extends HttpServlet
{
    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(Updater_servlet.class);

    @Inject
    private UpdatesModel updates;

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        ...
    }

      

Model class

@Model
public class UpdatesModel implements Serializable
{
    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(UpdatesModel.class);

    private final String url;
    private final String username;
    private final String password;
    private final String instpath;

    public UpdatesModel()
    {
        Properties prop = new Properties();

        try
        {
            Class.forName(driverName);
            prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
        }
        catch (ClassNotFoundException|IOException ex)
        {
            Logger.getLogger(UpdatesModel.class.getName()).log(Level.SEVERE, null, ex);
        }

        url          = "jdbc:mysql://" + serverName + "/" + prop.getProperty("database","");
        username     = prop.getProperty("dbuser","");
        password     = prop.getProperty("dbpassword","");

        instpath     = prop.getProperty("instpath","");
    }

      

The update field is never initialized. It is zero. What have I done wrong?

UPDATE: Dependencies:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.7</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.7</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.10.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>

      

I created empty beans.xml in WEB-INF - doesn't help ((I'm using Tomcat 7. Also tried to do tomcat 8 - no difference ((

+3


source to share


2 answers


First of all, a snippet of yours has pom.xml

absolutely nothing to do with the problem;) Some of the dependencies will be more interesting.

Create or verify that WEB-INF

there is an empty file named beans.xml

.

Make sure you have the cdi-api dependency installed in pom.xml

:

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
        <version>version</version>
    </dependency>

      



Also make sure you are deploying your war to a CDI enabled application server like Wildfly (JBoss), or make sure you have all the required libraries in the classpath if it's a simple servlet container like Tomcat.

It is also recommended to use concrete implementations for interfaces, so your class UpdatesModel

must implement some interface that defines its behavior. However, this is not required.

EDIT: welding dependency is only needed in SE environment. Everything should be in EE cdi-api

.

+3


source


I found out how to fix this problem. I added

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

      

in pom.xml and UpdaterRequestListener created



@WebListener
public class UpdaterRequestListener extends org.jboss.weld.environment.servlet.Listener
{
}

      

I wonder if I can do this without some additional welding dependency? Is there a javax alternative?

0


source







All Articles