Guice Servlet项目在启动时因IllegalAccessException而失败

时间:2010-07-29 19:05:09

标签: java servlets guice

我正在使用Guice servlet模块并试图让基本的过滤器和监听器运行。当我启动我的servlet容器时,我得到一个包含在AssertionError中的java.lang.IllegalAccessException。

基本上似乎正在发生的事情是Guice正在尝试使用公共构造函数实例化com.google.inject.servlet.ManagedServletPipeline,这是一个包私有类。我之前在使用包私有类时已经看过Guice的这个问题,解决方案一直只是简单地将构造函数的可见性从public更改为default。这里的问题是ManagedServletPipeline是一个框架类,因此我实际上无权修改它。我假设我做错了,因为这是在最基本的例子中出现的。

有谁知道如何解决这个问题?

完整的堆栈跟踪和相关文件包含在下面。仅供参考我想在Apache Tomcat v6上运行它。

的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"
    id="WebApp_ID" version="2.5">

    <display-name>guice-test</display-name>

    <listener>
        <listener-class>com.example.MyServletConfig</listener-class>
    </listener>

    <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>
</web-app>

MyServletConfig.java:

package com.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 MyServletConfig extends GuiceServletContextListener{

   @Override
   protected Injector getInjector(){
      return Guice.createInjector(new ServletModule());
   }
}

堆栈追踪:

java.lang.AssertionError: java.lang.IllegalAccessException: Class com.google.inject.DefaultConstructionProxyFactory$1 can not access a member of class com.google.inject.servlet.ManagedServletPipeline with modifiers "public"
    at com.google.inject.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:85)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
    at com.google.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:111)
    at com.google.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
    at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:811)
    at com.google.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
    at com.google.inject.Scopes$1$1.get(Scopes.java:54)
    at com.google.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:48)
    at com.google.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
    at com.google.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
    at com.google.inject.ConstructorInjector.construct(ConstructorInjector.java:84)
    at com.google.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:111)
    at com.google.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
    at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:811)
    at com.google.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
    at com.google.inject.Scopes$1$1.get(Scopes.java:54)
    at com.google.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:48)
    at com.google.inject.FactoryProxy.get(FactoryProxy.java:56)
    at com.google.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
    at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:811)
    at com.google.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
    at com.google.inject.Scopes$1$1.get(Scopes.java:54)
    at com.google.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:48)
    at com.google.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
    at com.google.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
    at com.google.inject.SingleMethodInjector.inject(SingleMethodInjector.java:84)
    at com.google.inject.InjectionRequestProcessor$StaticInjection$1.call(InjectionRequestProcessor.java:109)
    at com.google.inject.InjectionRequestProcessor$StaticInjection$1.call(InjectionRequestProcessor.java:106)
    at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:804)
    at com.google.inject.InjectionRequestProcessor$StaticInjection.injectMembers(InjectionRequestProcessor.java:106)
    at com.google.inject.InjectionRequestProcessor.injectMembers(InjectionRequestProcessor.java:74)
    at com.google.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:168)
    at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:113)
    at com.google.inject.Guice.createInjector(Guice.java:92)
    at com.google.inject.Guice.createInjector(Guice.java:69)
    at com.google.inject.Guice.createInjector(Guice.java:59)
    at com.example.MyServletConfig.getInjector(MyServletConfig.java:12)
    at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:43)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:593)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.IllegalAccessException: Class com.google.inject.DefaultConstructionProxyFactory$1 can not access a member of class com.google.inject.servlet.ManagedServletPipeline with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:505)
    at com.google.inject.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:81)
    ... 52 more

1 个答案:

答案 0 :(得分:3)

似乎使用guice-2.0.jar而不是guice-2.0-no_aop.jar修复了这个问题。

即使项目本身没有使用任何AOP功能,似乎servlet模块出于某种原因需要它。