添加Hibernate和Jandex后,Weld不再注册@ViewScoped bean

时间:2015-05-23 12:21:46

标签: hibernate jsf java-ee cdi jboss-weld

我使用带有JSF 2.2流程的CDI Weld容器。

    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet-core</artifactId>
        <version>2.2.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld</groupId>
        <artifactId>weld-core-jsf</artifactId>
        <version>2.2.11.Final</version>
    </dependency>

我使用带注释的发现模式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://xmlns.jcp.org/xml/ns/javaee" bean-discovery-mode = "annotated"/>

我也尝试在应用程序中使用hibernate最新的稳定版本

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.10.Final</version>
        </dependency>

此版本具有传递依赖org.jboss:jandex:1.1.0.Final,它会使Weld抛出异常

Caused by: java.lang.NoSuchMethodError: org.jboss.jandex.ClassInfo.hasNoArgsConstructor()Z
at org.jboss.weld.environment.deployment.discovery.jandex.JandexClassFileInfo.<init>(JandexClassFileInfo.java:65)
at org.jboss.weld.environment.deployment.discovery.jandex.JandexClassFileServices.getClassFileInfo(JandexClassFileServices.java:82)
at org.jboss.weld.bootstrap.FastAnnotatedTypeLoader.loadAnnotatedType(FastAnnotatedTypeLoader.java:64)
at org.jboss.weld.bootstrap.BeanDeployer.addClass(BeanDeployer.java:97)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer.java:65)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer.java:62)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 3 more

为了解决异常,我在classpath上添加了最新版本的jandex依赖

    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jandex</artifactId>
        <version>1.2.4.Final</version>
    </dependency>

在类路径中存在jandex时,Weld不会注册@ javax.faces.view.ViewScoped和@javax.faces.flow.FlowScoped bean。问题是类路径中存在org.jboss.jandex.Index对注册bean应用org.jboss.weld.environment.deployment.discovery.jandex.JandexDiscoveryStrategy策略。

org.jboss.weld.environment.deployment.discovery.DiscoveryStrategyFactory#create

    if (Reflections.isClassLoadable(resourceLoader, JANDEX_INDEX_CLASS_NAME)) {
        CommonLogger.LOG.usingJandex();
        try {
            return cast(classForName(resourceLoader, JANDEX_DISCOVERY_STRATEGY_CLASS_NAME).getConstructor(ResourceLoader.class, Bootstrap.class, Set.class)
                    .newInstance(resourceLoader, bootstrap, initialBeanDefiningAnnotations));
        } catch (Exception e) {
            throw CommonLogger.LOG.unableToInstantiate(JANDEX_DISCOVERY_STRATEGY_CLASS_NAME,
                    Arrays.toString(new Object[] { resourceLoader, bootstrap, initialBeanDefiningAnnotations }), e);
        }
    }
    return new ReflectionDiscoveryStrategy(resourceLoader, bootstrap, initialBeanDefiningAnnotations);

此策略不分析@ViewScoped和@FlowScoped bean注释。 最初使用org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy,它分析bean注释元信息。具体检查@ javax.enterprise.context.NormalScope

@NormalScope
@Inherited
@Documented
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface ViewScoped {
}

org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy#hasBeanDefiningAnnotation

    for (Class<? extends Annotation> metaAnnotation : metaAnnotations) {
        // The check is not perfomed recursively as bean defining annotations must be declared directly on a bean class
        // Also we don't cache the results and rely completely on the reflection optimizations
        if (hasBeanDefiningMetaAnnotationSpecified(clazz.getAnnotations(), metaAnnotation)) {
            return true;
        }
    }

请有任何想法来解决这个问题

1 个答案:

答案 0 :(得分:1)

这将在Weld 2.3.5.Final中修复。另请参阅WELD-2160

来自问题评论:

  

嗯,问题是javax.faces-2.2.12.jar不是bean存档 - 它   不包含beans.xml但包含可移植扩展 - 请参阅   还http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#bean_archive。   所以javax.faces-2.2.12.jar的内容被忽略了   javax.faces.view.ViewScoped未添加到bean定义集中   注释。确实如此   com.sun.faces.application.view.ViewScopeExtension正在使用   BeforeBeanDiscovery.addScope()将ViewScoped注册为作用域   注解。但是,Weld目前不支持这一点 - 请参阅   也是WELD-1624(由于鸡蛋般的问题)。我们应该改进   JandexDiscoveryStrategy识别注释注释的方式   @NormalScope。