gpt4 book ai didi

java - 模型未注入(inject)到 servlet 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:48 24 4
gpt4 key购买 nike

我有一个简单的 servlet 项目:我需要将依赖项注入(inject)一个 servlet 实例。我尝试了几种注释:DefaultSingltonEJBManagedBean,但没有任何帮助。

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>

小服务程序

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

更新字段永远不会被初始化。它等于空。我做错了什么?

更新:依赖项:

<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 - 没有帮助((我部署到 Tomcat 7。还尝试了 tomcat 8 - 没有区别((

最佳答案

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

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

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

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

还要确保将 war 部署到支持 CDI 的应用程序服务器(如 Wildfly (JBoss))中,或者如果它只是 servlet 容器(如 Tomcat),请确保在类路径中具有所有必要的库。

将具体实现注入(inject)接口(interface)也是一种很好的做法,因此您的 UpdatesModel 类应该实现一些指定其行为的接口(interface)。但这不是必需的。

编辑:只有在 SE 环境中才需要对焊接的依赖。在 EE 中,所有需要的应该是 cdi-api

关于java - 模型未注入(inject)到 servlet 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27556869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com