在osgi-container中找不到hibernate-validator的El实现

时间:2015-04-16 14:40:46

标签: java hibernate osgi hibernate-validator apache-servicemix

我尝试在osgi容器中运行Hibernate验证器。

<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>2.2.4</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.el</artifactId>
    <version>2.2.4</version>
</dependency>

<dependency>
    <groupId>org.apache.servicemix.bundles</groupId>
    <artifactId>org.apache.servicemix.bundles.hibernate-validator</artifactId>
    <version>5.0.2.Final_1</version>
</dependency>

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.7</version>
</dependency>

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.8.1</version>
</dependency>


public class HibernateValidationProviderResolver implements ValidationProviderResolver {
    @Override
    public List<ValidationProvider<?>> getValidationProviders() {
        List<ValidationProvider<?>> list = new ArrayList<>(1);
        list.add(new HibernateValidator());
        return list;
    }
}


Configuration<?> configuration = Validation.byDefaultProvider().providerResolver(
    new HibernateValidationProviderResolver()
).configure();

ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<Group>> constraintViolations = validator.validate(group);


public class Group {
    @NotNull
    @Size(min=2)
    private String title;
}

尝试运行,equinox控制台没问题:

10      RESOLVED    org.glassfish.web.javax.el_2.2.4
39      RESOLVED    org.apache.servicemix.bundles.hibernate-validator_5.0.2.Final_1
47      RESOLVED    javax.validation.api_1.1.0.Final
49      RESOLVED    javax.el-api_2.2.4

如果我使用title = null传递Group类实例,那么验证就可以了,而且constraintViolations包含一个违规&#34; not null&#34;。 如果我使用title = "A"传递Group类实例(一个字符对最小长度= 2),那么它会抛出异常

Caused by: javax.el.ELException: Provider com.sun.el.ExpressionFactoryImpl not found
Caused by: java.lang.ClassNotFoundException: com.sun.el.ExpressionFactoryImpl

它是由osgi引起的100%,但我应该如何在osgi中设置hibernate-validator?我能找到的所有文章都描述了创建HibernateValidationProviderResolver以及所有文章。

更新1

Maven: javax.el:javax.el-api:2.2.4

Export-Package: javax.el;version="2.2.4"
Import-Package: javax.el;version="2.2.4"

Maven: org.glassfish.web:javax.el:2.2.4 MANIFEST.MF

Export-Package: com.sun.el;uses:="javax.el";version="2.2.4"
Private-Package: com.sun.el.lang;version="2.2.4",com.sun.el.parser;version="2.2.4",com.sun.el.util;version="2.2.4"
Import-Package: com.sun.el;version="2.2.4",javax.el;version="2.2"

Maven: org.apache.servicemix.bundles:org.apache.servicemix.bundles.hibernate-validator:5.0.2.Final_1

Implementation-Version: 5.0.2.Final
Import-Package: javax.el,javax.persistence;resolution:=optional, ...

Export-Package: org.hibernate.validator.internal.engine.messageinterpola
tion.el;uses:="javax.el,javax.validation,org.hibernate.validator.intern
al.engine.messageinterpolation";version="5.0.2.Final",org.hibernate.val
idator.internal.engine.messageinterpolation;uses:="javax.validation.met
adata,org.hibernate.validator.internal.engine.messageinterpolation.el,j
avax.el,javax.validation,org.hibernate.validator.internal.util.logging"
;version="5.0.2.Final", ...

在hibernate bundle中导入的任何版本,在el-api和el-impl和el-impl导出中的2.2.4导入el-api为2.2,而不是2.2.4。所有捆绑包都已解决。

更新2

决定1

我实施了@ hwellmann的想法。 @hwellmann,这是对的吗?

public void createGroup(Group group) {
    ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();

    try {
        ClassLoader[] classLoaders = new ClassLoader[] {
            prevClassLoader,
            ExpressionFactoryImpl.class.getClassLoader()
        };

        // build composite classloader

        Thread.currentThread().setContextClassLoader(compositeClassLoader);

        Set<ConstraintViolation<Group>> constraintViolations = validator.validate(group);

    } finally {
        Thread.currentThread().setContextClassLoader(prevClassLoader);
    }
}

它有效,但看起来很奇怪。并且在每个验证处理上更改TCCL看起来都是一些开销。

决定2

当我将自己的消息属性添加到每个验证注释时,错误已经消失,例如对于Group:

public class Group {
    @NotNull
    @Size(min=2, message="field.too_short")
    private String title;
}

在这种情况下似乎没有启动hibernate插值器,因此不会从TCCL中检索ExpressionFactoryImpl(之前我们读过min = 2的值,现在我们不会这样做)。如果对我们没问题,这个决定是最简单的。 我将进一步研究这个领域并在那里分享我的观察结果。

3 个答案:

答案 0 :(得分:3)

JBoss Fuse中存在一个相关问题: https://access.redhat.com/solutions/1479723

他们建议降级为4.3.1.Final

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>4.3.1.Final</version>
</dependency>

答案 1 :(得分:1)

完整的堆栈跟踪可能提供更多的洞察力,而不仅仅是异常消息。

我的猜测是,您通过META-INF/service/javax.el.ExpressionFactory看到了服务查询失败的结果。正在进行查找的捆绑包显然无法看到com.sun.el

将此包导入应用程序包并将线程上下文类加载器设置为bundle classloader可能会有所帮助。

答案 2 :(得分:0)

我遇到了与Hibernate 5.2.5和Jetty 9.x类似的问题。基本上,看起来Hibernate没有检查类路径上是否已经有EL实现,并尝试使用javax.el-api而不是Jetty 9的apache-el。升级到Hibernate 5.2.10为我解决了这个问题。在类路径上不再有冲突的el-api。