0

我试图让一个简单的框架运行起来,但我在让它实际运行时遇到了一些麻烦。

以下文件都在“示例”包中:

抽象控制器.java

package example;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Singleton;


@Singleton
@SuppressWarnings("serial")
public abstract class AbstractController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

ApplicationInitializer.java

package example;

import com.google.inject.Inject;
//import com.google.inject.persist.PersistService;

public class ApplicationInitializer {
    @Inject
    ApplicationInitializer() {
        // stuff comes here later
    }
}

GuiceServletConfig.java

package example;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;

public class GuiceServletConfig extends GuiceServletContextListener {       
    @Override
    protected Injector getInjector() {

        Injector injector = Guice.createInjector(new ServletModule() {
            @Override
            protected void configureServlets() {        

                // direct to servlets
                serve("/Example").with(ServerController.class);
                serve("/*").with(ServerController.class);


            }
        }/*, new ProductionModule()*/);

        injector.getInstance(ApplicationInitializer.class);

        return injector;
    }

}

生产模块.java

package example;

import com.google.inject.AbstractModule;

public class ProductionModule extends AbstractModule {
    @Override
    protected void configure() {
        // stuff comes later
    }
}

服务器控制器.java

package example;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Singleton;

@Singleton
@SuppressWarnings("serial")
public class ServerController extends AbstractController {

    //@Inject
    public ServerController() {
        // future dependencies
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("WEB-INF/pages/test.jsp").forward(request, response);
    }

}

我已将以下内容添加到我的 web.xml 顶部附近:

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

    <listener>
        <listener-class>example.GuiceServletConfig</listener-class>
    </listener>

但由于某种原因,我得到以下堆栈跟踪:(在 Tomcat 上运行,作为服务)

Aug 06, 2013 3:58:44 PM org.apache.catalina.core.AprLifecycleListener init
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.7.0_21\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Java\jre7\bin;C:\Program Files\Java\jre7\bin\.;C:\Program Files\Java\jdk1.7.0_21\bin;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\eclipse;;.
Aug 06, 2013 3:58:45 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CIMPUI' did not find a matching property.
Aug 06, 2013 3:58:45 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MyExample' did not find a matching property.
Aug 06, 2013 3:58:45 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 06, 2013 3:58:45 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 06, 2013 3:58:45 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 371 ms
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.41
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class MyExample.GuiceServletConfig
java.lang.ClassNotFoundException: MyExample.GuiceServletConfig
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4823)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [] startup failed due to previous errors
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class MyExample.GuiceServletConfig
java.lang.ClassNotFoundException: MyExample.GuiceServletConfig
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4823)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/CIMPUI] startup failed due to previous errors
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class MyExample.GuiceServletConfig
java.lang.ClassNotFoundException: MyExample.GuiceServletConfig
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4823)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Aug 06, 2013 3:58:45 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/MyExample] startup failed due to previous errors
Aug 06, 2013 3:58:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 06, 2013 3:58:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 06, 2013 3:58:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 557 ms

我已经对此进行了很长一段时间的故障排除,并且希望让它真正做一些除了抛出异常之外的事情。有任何想法吗?

4

1 回答 1

0

正如错误所说:

SEVERE: Error configuring application listener of class MyExample.GuiceServletConfig
    java.lang.ClassNotFoundException: MyExample.GuiceServletConfig

您的听众不存在:

<listener>
    <listener-class>MyExample.GuiceServletConfig</listener-class>
</listener>

错误的包装;正如您所说,您的课程都在example包中。

于 2013-08-06T20:15:01.990 回答