编程语言
首页 > 编程语言> > java-模型未注入servlet

java-模型未注入servlet

作者:互联网

我有一个简单的servlet项目:
我需要注入对servlet实例的依赖.我尝试了几种注释:Default,Singlton,EJB,ManagedBean,没有任何帮助.

POM.xml的一部分

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

Servlet

@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
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","");
    }

更新字段永远不会初始化.等于null.我做错了什么?

更新:
依赖项:

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

我在WEB-INF中创建了空的beans.xml-没有帮助((
我部署到Tomcat7.还尝试了tomcat 8-没什么不同((

解决方法:

首先,您的pom.xml块与该问题完全无关;)依赖关系部分会更有趣.

在WEB-INF目录中创建或确保它存在一个名为beans.xml的空文件.

确保在pom.xml中定义了cdi-api依赖项:

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

此外,还应确保将战争部署到启用了CDI的应用服务器(如Wildfly(JBoss))中,或者如果类路径仅是Tomcat之类的servlet容器,请确保类路径上具有所有必需的库.

将具体的实现注入接口也是一种很好的做法,因此UpdatesModel类应实现一些接口以指定其行为.但这不是必需的.

编辑:仅在SE环境中才需要焊接的依赖关系.在EE中,所有需要的都是cdi-api.

标签:cdi,servlets,java
来源: https://codeday.me/bug/20191120/2047799.html