我有一个简单的java ee应用程序,在pom.xml中有以下依赖项
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
我想把一个bean注入我的servlet但是在tomcat8中运行这个应用时总是会得到空指针异常
package di;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class Apple {
public Apple(){}
public String getNewApple(){
return "apple";
}
}
package main;
import di.Apple;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "hello", urlPatterns = {"/hello"})
public class Main extends HttpServlet {
@Inject
Apple apple;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("hello");
out.println(apple.getNewApple());
} finally {
out.close();
}
}
}
当我将其部署到tomEE plus 7.0.1时,我的应用程序启动但该页面为空白。以下是部署日志:
C:\apache-tomee-plus-7.0.1\bin\catalina.bat run
[2016-06-28 02:02:25,349] Artifact TOMEE PLUS:Web exploded: Server is not connected. Deploy is not available.
Using CATALINA_BASE: "C:\Users\User\.IntelliJIdea14\system\tomcat\Unnamed_TOMEE_PLUS"
Using CATALINA_HOME: "C:\apache-tomee-plus-7.0.1"
Using CATALINA_TMPDIR: "C:\apache-tomee-plus-7.0.1\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_60"
Using CLASSPATH: "C:\apache-tomee-plus-7.0.1\bin\bootstrap.jar;C:\apache-tomee-plus-7.0.1\bin\tomcat-juli.jar"
INFO - Server version: Apache Tomcat (TomEE)/8.5.3 (7.0.1)
INFO - Server built: Jun 9 2016 11:16:29 UTC
INFO - Server number: 8.5.3.0
INFO - OS Name: Windows 7
INFO - OS Version: 6.1
INFO - Architecture: x86
INFO - Java Home: C:\Program Files\Java\jdk1.8.0_60\jre
INFO - JVM Version: 1.8.0_60-b27
INFO - JVM Vendor: Oracle Corporation
INFO - CATALINA_BASE: C:\Users\User\.IntelliJIdea14\system\tomcat\Unnamed_TOMEE_PLUS
INFO - CATALINA_HOME: C:\apache-tomee-plus-7.0.1
INFO - Command line argument: -javaagent:C:\apache-tomee-plus-7.0.1\lib\openejb-javaagent.jar
INFO - Command line argument: -Dcom.sun.management.jmxremote=
INFO - Command line argument: -Dcom.sun.management.jmxremote.port=1099
INFO - Command line argument: -Dcom.sun.management.jmxremote.ssl=false
INFO - Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
INFO - Command line argument: -Djava.rmi.server.hostname=127.0.0.1
INFO - Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
INFO - Command line argument: -Djava.util.logging.config.file=C:\Users\User\.IntelliJIdea14\system\tomcat\Unnamed_TOMEE_PLUS\conf\logging.properties
INFO - Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
INFO - Command line argument: -Dcatalina.base=C:\Users\User\.IntelliJIdea14\system\tomcat\Unnamed_TOMEE_PLUS
INFO - Command line argument: -Dcatalina.home=C:\apache-tomee-plus-7.0.1
INFO - Command line argument: -Djava.io.tmpdir=C:\apache-tomee-plus-7.0.1\temp
INFO - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_60\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_80\bin\;C:\Program Files\Skype\Phone\;C://MAVEN/BIN;C:\Users\User\AppData\Local\atom\bin;C:\Program Files\Microsoft VS Code\bin;C:\gradle-2.13\bin;.
INFO - Initializing ProtocolHandler ["http-nio-8080"]
INFO - Using a shared selector for servlet write/read
INFO - Initializing ProtocolHandler ["ajp-nio-8009"]
INFO - Using a shared selector for servlet write/read
INFO - Using 'openejb.jdbc.datasource-creator=org.apache.tomee.jdbc.TomEEDataSourceCreator'
INFO - ********************************************************************************
INFO - OpenEJB http://tomee.apache.org/
INFO - Startup: Tue Jun 28 02:02:33 EEST 2016
INFO - Copyright 1999-2016 (C) Apache OpenEJB Project, All Rights Reserved.
INFO - Version: 7.0.1
INFO - Build date: 20160623
INFO - Build time: 12:04
INFO - ********************************************************************************
INFO - openejb.home = C:\apache-tomee-plus-7.0.1
INFO - openejb.base = C:\Users\User\.IntelliJIdea14\system\tomcat\Unnamed_TOMEE_PLUS
INFO - Created new singletonService org.apache.openejb.cdi.ThreadSingletonServiceImpl@e7dcfd
INFO - Succeeded in installing singleton service
INFO - TomEE configuration file is 'C:\Users\User\.IntelliJIdea14\system\tomcat\Unnamed_TOMEE_PLUS\conf\tomee.xml'
INFO - Configuring Service(id=Tomcat Security Service, type=SecurityService, provider-id=Tomcat Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Using 'openejb.deployments.classpath=false'
INFO - Creating TransactionManager(id=Default Transaction Manager)
INFO - Creating SecurityService(id=Tomcat Security Service)
INFO - Creating ServerService(id=cxf)
INFO - Creating ServerService(id=cxf-rs)
INFO - ** Bound Services **
INFO - NAME IP PORT
INFO - -------
INFO - Ready!
INFO - Initialization processed in 6345 ms
INFO - Importing a Tomcat Resource with id 'UserDatabase' of type 'org.apache.catalina.UserDatabase'.
INFO - Creating Resource(id=UserDatabase)
INFO - Starting service Catalina
INFO - Starting Servlet Engine: Apache Tomcat (TomEE)/8.5.3 (7.0.1)
INFO - Starting ProtocolHandler [http-nio-8080]
INFO - Starting ProtocolHandler [ajp-nio-8009]
INFO - Server startup in 268 ms
INFO - Deploying web application directory C:\apache-tomee-plus-7.0.1\webapps\manager
INFO - ------------------------- localhost -> /manager
INFO - Configuring enterprise application: C:\apache-tomee-plus-7.0.1\webapps\manager
INFO - Enterprise application "C:\apache-tomee-plus-7.0.1\webapps\manager" loaded.
INFO - Assembling app: C:\apache-tomee-plus-7.0.1\webapps\manager
INFO - using context file C:\apache-tomee-plus-7.0.1\webapps\manager\META-INF\context.xml
INFO - Deployed Application(path=C:\apache-tomee-plus-7.0.1\webapps\manager)
INFO - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
INFO - Deployment of web application directory C:\apache-tomee-plus-7.0.1\webapps\manager has finished in 1,391 ms
任何人都可以建议如何解决这个问题。比你
答案 0 :(得分:0)
tomee plus +
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>