samedi 23 mai 2015

Weld does not register @ViewScoped beans anymore after adding Hibernate and Jandex

I use CDI Weld container with JSF 2.2 flow.

    <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>

I use annotated discovery mode

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://ift.tt/19L2NlC" bean-discovery-mode = "annotated"/>

I also try to use hibernate in the application the latest stable version

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

This version has a transitive dependency org.jboss:jandex:1.1.0.Final which renders Weld throws an exception

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

To get around exception, I added on classpath the latest version of jandex dependency

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

In the presence of jandex in classpath Weld not register @javax.faces.view.ViewScoped and @javax.faces.flow.FlowScoped beans. The problem is that the presence of org.jboss.jandex.Index in classpath applies org.jboss.weld.environment.deployment.discovery.jandex.JandexDiscoveryStrategy strategy for register beans.

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);

This strategy does not analyze @ViewScoped and @FlowScoped bean annotations. Initially used org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy, which analyze bean annotation meta info. Specifically checks for @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;
        }
    }

Please any ideas to get around this problem

Aucun commentaire:

Enregistrer un commentaire