开发者

Guice Servlet project fails with IllegalAccessException on startup

I'm using the Guice servlet module and trying to get just the basic filter and listener running. When I start my servlet container I get a java.lang.IllegalAccessException wrapped in an AssertionError.

Basically what appears to be happening is that Guice is trying to instantiate com.google.inject.servlet.ManagedServletPipeline which is a package-private class using a public constructor. I've seen this issue with Guice before when I use package-private classes, and the solution has always been to simply change the visibility of the constructor from public to default. The problem here is that ManagedServletPipeline is a framework class and thus I don't really have access to modify it. I assume I'm doing something wrong since this is showing up in the most basic of examples.

Does anyone know how I can work around this issue?

The full stack-trace and relevant files are included below. FYI I'm trying to run this on 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());
   }
}

Stack Trace:

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.g开发者_运维技巧oogle.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


It seems that using the guice-2.0.jar instead of guice-2.0-no_aop.jar fixes this issue.

Even though the project itself isn't using any AOP features, it seems the servlet module requires it for some reason.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜