org.slf4j.impl.StaticLoggerBinder - java.lang.IllegalAccessException:只有某些类可以访问此方法

时间:2016-07-05 22:52:48

标签: java static classloader logback slf4j

在我们的应用程序(App1)中,我们使用logback slf4j和另一个团队的jar(App2)。 App2也使用logback。我们的logback版本比他们的版本更新。我们已经在我们的专家的罐子里添加了排除。

注意:添加了-verbose:class并确保只有我们的logback jar正在加载

在weblogic上部署App1,我们在运行时收到以下异常:

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.IllegalAccessException: Only certain classes can access this method.
    at ch.qos.logback.classic.util.ContextSelectorStaticBinder.init(ContextSelectorStaticBinder.java:59)
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:92)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at eg.App2.somepkg.App2sServletContextListener.<clinit>(App2sServletContextListener.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at weblogic.servlet.internal.WebComponentContributor.getNewInstance(WebComponentContributor.java:252)
    at weblogic.servlet.internal.WebComponentContributor.createInstance(WebComponentContributor.java:297)
    at weblogic.servlet.internal.WebAppServletContext.createInstance(WebAppServletContext.java:4209)
    at weblogic.servlet.internal.WebAppServletContext.createInstance(WebAppServletContext.java:4197)
    at weblogic.servlet.internal.EventsManager.registerEventListener(EventsManager.java:538)
    at weblogic.servlet.internal.EventsManager.registerEventListeners(EventsManager.java:139)
    at weblogic.servlet.internal.EventsManager.registerEventListeners(EventsManager.java:97)
    at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1908)
    at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3063)
    at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1830)
    at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:875)
    at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)
    at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:356)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:138)
    at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:237)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:232)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:82)
    at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:52)
    at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:753)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:263)
    at weblogic.application.internal.EarDeployment.activate(EarDeployment.java:67)
    at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
    at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:80)
    at weblogic.deploy.internal.targetserver.BasicDeployment.activate(BasicDeployment.java:265)
    at weblogic.deploy.internal.targetserver.BasicDeployment.activateFromServerLifecycle(BasicDeployment.java:481)
    at weblogic.management.deploy.internal.DeploymentAdapter$1.doActivate(DeploymentAdapter.java:53)
    at weblogic.management.deploy.internal.DeploymentAdapter.activate(DeploymentAdapter.java:202)
    at weblogic.management.deploy.internal.AppTransition$2.transitionApp(AppTransition.java:52)
    at weblogic.management.deploy.internal.ConfiguredDeployments$2.doItem(ConfiguredDeployments.java:592)
    at weblogic.management.deploy.internal.parallel.BucketInvoker.invoke(BucketInvoker.java:138)
    at weblogic.management.deploy.internal.ConfiguredDeployments.transitionAppsParallel(ConfiguredDeployments.java:600)
    at weblogic.management.deploy.internal.ConfiguredDeployments.activate(ConfiguredDeployments.java:259)
    at weblogic.management.deploy.internal.ConfiguredDeployments.deploy(ConfiguredDeployments.java:155)
    at weblogic.management.deploy.internal.DeploymentServerService.resume(DeploymentServerService.java:221)
    at weblogic.management.deploy.internal.DeploymentServerService.start(DeploymentServerService.java:130)
    at weblogic.server.AbstractServerService.postConstruct(AbstractServerService.java:76)
    at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.glassfish.hk2.utilities.reflection.ReflectionHelper.invoke(ReflectionHelper.java:1262)
    at org.jvnet.hk2.internal.ClazzCreator.postConstructMe(ClazzCreator.java:332)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:374)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:228)
    at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:85)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2072)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:698)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:78)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:228)
    at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:85)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2072)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
    at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:88)
    at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1213)
    at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1144)
    at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:643)
    at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
    at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
    at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
    at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
    at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:617)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:397)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)
weblogic.application.ModuleException: java.lang.IncompatibleClassChangeError: Class ch.qos.logback.classic.LoggerContext does not implement the requested interface org.slf4j.ILoggerFactory
    at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)
    at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:237)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:232)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    Truncated. see log file for complete stacktrace
Caused By: java.lang.IncompatibleClassChangeError: Class ch.qos.logback.classic.LoggerContext does not implement the requested interface org.slf4j.ILoggerFactory
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at eg.App2.somepkg.App2sServletContextListener.<clinit>(App2sServletContextListener.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    Truncated. see log file for complete stacktrace
> 

看着

ch.qos.logback.classic.util.ContextSelectorStaticBinder.init(LoggerContext, Object)

org.slf4j.impl.StaticLoggerBinder.init()

我怀疑这个变量是罪魁祸首

org.slf4j.impl.StaticLoggerBinder.KEY

这个private static variable有什么用?

单件类中这段代码的需求是什么?

if (this.key == null) {
    this.key = key;
} else if (this.key != key) {
    throw new IllegalAccessException("Only certain classes can access this method.");
}

修改/解决方法: 我在weblogic-application.xml&{39} <prefer-application-resources><prefer-application-packages>

中只有以下内容
<resource-name>org.slf4j.*</resource-name>

添加这个也解决了这个问题 - 不再有IllegalAccessException,但仍然不清楚为什么/如何!?

<resource-name>ch.qos.*</resource-name>

0 个答案:

没有答案