焊接在加载自定义上下文类时遇到NoClassDefFoundError

时间:2014-04-10 10:42:28

标签: glassfish cdi weld

我正在编写一个通过扩展程序添加的CDI自定义作用域:

public class SliceScopeExtension
        implements Extension, Serializable {

  public void addScope(@Observes final BeforeBeanDiscovery event) {
    event.addScope(SliceScoped.class, true, true);
  }

  public void registerContext(@Observes final AfterBeanDiscovery event) {
    event.addContext(new SliceScopeContext());
  }
}

相应的上下文类使用派生自org.jboss.weld.context.beanstore.AttributeBeanStore

的自定义bean存储实现
public class SliceScopeContext implements Context, Serializable {
  private static final Logger log = Logger.getLogger(SliceScopeContext.class.getName());

  private final ThreadLocal<HttpServletRequest> request;
  private final ThreadLocal<SliceScopeBeanStore> beanStore;

  public SliceScopeContext() {
    this.request = new ThreadLocal<>();
    this.beanStore = new ThreadLocal<>();
  }
...
}

如果我现在部署此Web应用程序,glassfish / weld会抛出一个org.jboss.weld.resources.spi.ResourceLoadingException,其中嵌套NoClassDefFoundError表示无法找到类org/jboss/weld/context/beanstore/AttributeBeanStore。如果我使用NamingScheme接口或SimpleNamingScheme,则会为这些类名引发错误:

org.jboss.weld.resources.spi.ResourceLoadingException: Error loading class my.package.SliceScopeContext
  at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:179)
  at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:188)
  at org.jboss.weld.bootstrap.BeanDeployer.loadAnnotatedType(BeanDeployer.java:122)
  at org.jboss.weld.bootstrap.BeanDeployer.addClass(BeanDeployer.java:100)
  at org.jboss.weld.bootstrap.BeanDeployer.addClasses(BeanDeployer.java:159)
  at org.jboss.weld.bootstrap.BeanDeployment.createClasses(BeanDeployment.java:214)
  at org.jboss.weld.bootstrap.WeldBootstrap.startInitialization(WeldBootstrap.java:470)
  at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:211)
  at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
  at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
  at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:493)
  at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
  at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
  at java.security.AccessController.doPrivileged(Native Method)
  at javax.security.auth.Subject.doAs(Subject.java:356)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
  at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
  at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
  at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
  at org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
  at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
  at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
  at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
  at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
  at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
  at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
  at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NoClassDefFoundError: org/jboss/weld/context/beanstore/AttributeBeanStore
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
  at org.glassfish.web.loader.WebappClassLoader.findClass(WebappClassLoader.java:1183)
  at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1728)
  at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:266)
  at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
  at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
  at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
  at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
  at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
  at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
  at sun.reflect.generics.repository.FieldRepository.getGenericType(FieldRepository.java:85)
  at java.lang.reflect.Field.getGenericType(Field.java:236)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedField.of(BackedAnnotatedField.java:28)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.computeValue(BackedAnnotatedType.java:178)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.computeValue(BackedAnnotatedType.java:171)
  at org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:35)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$EagerlyInitializedLazyValueHolder.<init>(BackedAnnotatedType.java:154)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.<init>(BackedAnnotatedType.java:171)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedFields.<init>(BackedAnnotatedType.java:171)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.<init>(BackedAnnotatedType.java:65)
  at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.of(BackedAnnotatedType.java:47)
  at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.apply(ClassTransformer.java:85)
  at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.apply(ClassTransformer.java:82)
  at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:358)
  at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
  at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
  at com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
  at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:396)
  at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:174)
  ... 43 more
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.context.beanstore.AttributeBeanStore
  at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1761)
  at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
  ... 76 more

我验证了这些类是否存在于glassfish weld-osgi-bundle.jar目录中的modules中,并且它们是。 This SO post我已经检查过,我的项目不包含任何已包含这些WELD类的库。

这里发生了什么或我做错了什么?

1 个答案:

答案 0 :(得分:1)

打开weld-osgi-bundle.jar,您可以在META-INF/MANIFEST.MF文件中看到Export-Package部分未提及org.jboss.weld.context.beanstore;包,因此焊接是私密的。

您可能必须通过复制此焊接代码或使用Apache Deltaspike和org.apache.deltaspike.core.util.context中的工具来创建您自己的上下文,以创建跨Weld和OpenWebBeans的可移植上下文。 最后一个(脏)解决方案是修改MANIFEST.MF

中的weld-osgi-bundle.jar文件