gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:20:22 24 4
gpt4 key购买 nike

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

基本上,似乎正在发生的事情是 Guice 正在尝试实例化 com.google.inject.servlet.ManagedServletPipeline,这是一个使用公共(public)构造函数的包私有(private)类。我之前在使用包私有(private)类时曾在 Guice 中看到过这个问题,解决方案一直是简单地将构造函数的可见性从公共(public)更改为默认。这里的问题是 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

最佳答案

似乎使用 guice-2.0.jar 而不是 guice-2.0-no_aop.jar 可以解决这个问题。

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

关于java - Guice Servlet 项目在启动时因 IllegalAccessException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3366063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com