我尝试使用Google App Engine配置Guice,但是当我启动服务器时出现此问题(使用" gradle appengineRun"):
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/internal/util/$Preconditions
at com.google.inject.servlet.ServletModule.configure(ServletModule.java:44)
at com.google.inject.AbstractModule.configure(AbstractModule.java:62)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340)
at com.google.inject.spi.Elements.getElements(Elements.java:110)
at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:138)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:104)
at com.google.inject.Guice.createInjector(Guice.java:96)
at com.google.inject.Guice.createInjector(Guice.java:73)
at com.google.inject.Guice.createInjector(Guice.java:62)
at MyGuiceServletConfig.getInjector(MyGuiceServletConfig.java:17)
at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:843)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:533)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:816)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:345)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1406)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1368)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:778)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:262)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:522)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:131)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:105)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:131)
at org.eclipse.jetty.server.Server.start(Server.java:422)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:105)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.Server.doStart(Server.java:389)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at com.google.appengine.tools.development.jetty9.JettyContainerService.startContainer(JettyContainerService.java:346)
at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:284)
at com.google.appengine.tools.development.AutomaticInstanceHolder.startUp(AutomaticInstanceHolder.java:26)
at com.google.appengine.tools.development.AbstractModule.startup(AbstractModule.java:87)
at com.google.appengine.tools.development.Modules.startup(Modules.java:105)
at com.google.appengine.tools.development.DevAppServerImpl.doStart(DevAppServerImpl.java:274)
at com.google.appengine.tools.development.DevAppServerImpl.access$000(DevAppServerImpl.java:47)
at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:219)
at com.google.appengine.tools.development.DevAppServerImpl$1.run(DevAppServerImpl.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:217)
at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:368)
at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:47)
at com.google.appengine.tools.development.DevAppServerMain.run(DevAppServerMain.java:223)
at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:214)
Caused by: java.lang.ClassNotFoundException: com.google.inject.internal.util.$Preconditions
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 46 more
项目结构:
服务器和Infrastructure部分仅包含一些DAO和Factory元素
的web.xml:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--<servlet>-->
<!--<servlet-name>hello</servlet-name>-->
<!--<servlet-class>Main</servlet-class>-->
<!--<!–</servlet>–>-->
<!--<servlet-mapping>-->
<!--<servlet-name>hello</servlet-name>-->
<!--<url-pattern>/data</url-pattern>-->
<!--</servlet-mapping>-->
<listener>
<listener-class>
MyContextListener
</listener-class>
</listener>
<listener>
<listener-class>MyGuiceServletConfig</listener-class>
</listener>
</web-app>
MyContextListener应该用一些初始数据加载数据库,并且工作正常,但我不知道我是否可以将它与MyGuiceServletConfig一起使用。
MyGuiceServletConfig.java:
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.RequestScoped;
import com.google.inject.servlet.ServletModule;
import edu.ubb.cs.idde.DAO.EmployeeDAOHibernatePersistence;
import edu.ubb.cs.idde.DAO.EmployeesDAO;
import edu.ubb.cs.idde.Employees.Employee;
public class MyGuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule(){
@Override
protected void configureServlets(){
serve("/data").with(Main.class);
bind(EmployeesDAO.class).annotatedWith(Names.named("HibernatePersistence")).to(EmployeeDAOHibernatePersistence.class);
}
});
}
}
Main.java:
import com.google.gson.Gson;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import edu.ubb.cs.idde.DAO.EmployeeDAOHibernatePersistence;
import edu.ubb.cs.idde.DAO.EmployeesDAO;
import edu.ubb.cs.idde.DAO.EmployeesDAOHibernate;
import edu.ubb.cs.idde.Employees.Employee;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Singleton
public class Main extends HttpServlet {
EmployeesDAO empl;
@Inject
Main(EmployeesDAO employeesDAO)
{
this.empl = new EmployeeDAOHibernatePersistence();
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
Gson gson = new Gson();
List<Object> list = empl.readAllEmployees();
out.println(gson.toJson(list));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
Gson gson = new Gson();
// int ID =
Integer.parseInt(String.valueOf(req.getParameterValues("ID")));
String first = String.valueOf(req.getParameterValues("First"));
String last = String.valueOf(req.getParameterValues("Last"));
int age =
Integer.parseInt(String.valueOf(req.getParameterValues("Age")));
empl.createEmployee(new Employee(first,last,age));
out.println("OK");
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
Gson gson = new Gson();
int ID = Integer.parseInt(String.valueOf(req.getParameterValues("ID")));
String first = String.valueOf(req.getParameterValues("First"));
String last = String.valueOf(req.getParameterValues("Last"));
int age =
Integer.parseInt(String.valueOf(req.getParameterValues("Last")));
empl.updateEmployee(new Employee(first,last,age));
out.println("OK");
}
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
Gson gson = new Gson();
int ID = Integer.parseInt(String.valueOf(req.getParameterValues("ID")));
String first = String.valueOf(req.getParameterValues("First"));
String last = String.valueOf(req.getParameterValues("Last"));
int age =
Integer.parseInt(String.valueOf(req.getParameterValues("Last")));
empl.deleteEmployee(new Employee(first,last,age));
out.println("OK");
}
}
答案 0 :(得分:0)
基于其他similar situations,您似乎使用的是Guice的过时版本,或者至少是Guice的扩展版,它与版本库的其余部分不兼容。
确保为Guice库及其扩展使用相同的版本(最新版本为4.1)。
无论如何,无论何时部署应用,请按照here所述的步骤进行操作。