容器未找到.jar内的CDI Bean(不满意的依赖项) [英] CDI beans inside .jar are not found by the container (Unsatisfied dependencies)

查看:97
本文介绍了容器未找到.jar内的CDI Bean(不满意的依赖项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Java项目来充当其他项目的库,从而减少了项目之间的代码重复.此lib项目将导出到 jar ,以包含在Web项目中(WAR,而不是EAR).

I have created a Java project to serve as a lib to other projects, reducing code duplication between projects. This lib project is exported to jar to be included in Web projects (WAR, not EAR).

在Web项目中(这些类已被删除),一切正常,而所有类都保留在它们上-简单对象和复杂对象(带有生产者和设置的对象)的注入正常工作

In Web projects (where these classes are being removed) everything worked as usual while all classes were kept on them ─ the injection of simple and complex objects (those with Producers and settings) was working normally.

在删除了这些Web项目类之后,将具有相同类的jar添加到Web项目中(在Maven项目中的pom.xml中设置了这个lib),所有内容都可以像以前一样正常编译.但是在启动服务器时,容器在CDI启动期间找不到jar中现在存在的类(CDI bean),从而产生此(著名的)错误:

After removing these classes of the Web projects and add the jar with these same classes to the Web projects (setting this lib in pom.xml in Maven projects) everything is compiled normally, as it was before too. But on start the server, classes (CDI beans) now present in the jar are not found by the container during startup of CDI, generating this (famous) error:

WELD-001408: Unsatisfied dependencies for type Session with qualifiers (...)

已经在src/main/resources(在WELD和CDI文档中指示)中的META-INF文件夹中添加了 beans.xml 作为项目的根文件夹.

Already added the beans.xml in the META-INF folder both in the src/main/resources (indicated in WELD and CDI documentation) as the root folder of the project.

下面是在jar中显示的Bean(会话,SessionFactory和ExampleLogger)的示例,需要将其注入到其他项目中(并且该类在Web项目中时已正常工作),但现在不是被CDI发现:

Below are examples of beans (Session, SessionFactory and ExampleLogger) presents in the jar that needs to be injected into other projects (and have worked normally while the class was in the Web projects) but now is not being discovered by the CDI:

public class HibernateConnectionFactory {

    @Produces
    @ApplicationScoped @ConnectionBaseExample
    public SessionFactory createSessionFactoryExample() {
        Configuration configuration = new Configuration();
        configurarSessionFactory(configuration, "baseExampleDS");
        ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        return configuration.buildSessionFactory(registry);
    }

    @Produces
    @RequestScoped @ConnectionBaseExample
    public Session createSessionExample(@ConnectionBaseExample SessionFactory sessionFactory) {
        return sessionFactory.openSession();
    }

    public void destruirSessionExemplo(@Disposes @ConnectionBaseExample Session session) {
        if (session.isOpen()) {
            session.close();
        }
    }
}

public class ExampleLoggerProducer {

    @Produces
    public ExampleLogger createLogger(InjectionPoint injectionPoint) {
        return new ExampleLogger(injectionPoint.getMember().getDeclaringClass());
    }
}

该问题发生在Maven项目和非Maven项目中.有人遇到过这个问题吗?有人知道容器中找到 jar 中存在的bean的解决方案吗?预先感谢您,英语不好.

The problem occurs in the Maven projects and non-Maven projects as well. Has anyone faced this problem? Did anyone know a solution to the beans present in the jar be found by the container? Thank you in advance and sorry for the bad English.

Java EE7,CDI 1.1,WELD 2.1,服务器WildFly 8.1

Java EE7, CDI 1.1, WELD 2.1, server WildFly 8.1

推荐答案

找到解决方案后再试一次─我将 beans.xml 放在了项目根目录文件夹就可以了!这与在WELD文档中有关如何在jar中使用CDI bean的描述背道而驰,其中说将bean.xml放置在src/main/resources/META-INF内,但是可以.也许在某些Maven项目中是这样.

Trying again after a while I've found the solution ─ I placed the beans.xml on the META-INF folder inside the project root folder and it worked! That's against the description of how to work with CDI beans inside jars on WELD documentation, where says the place to put beans.xml is inside src/main/resources/META-INF, but is okay. Maybe in some Maven projects, that's true.

[UPDATE] 之所以有效,是因为我使用Eclipse的导出jar向导而不是使用Maven来构建jar.使用Maven应该可以与src/main/resources/META-INF中的beans.xml配合使用.

[UPDATE] This works because I was building the jar using the export jar wizard of Eclipse instead of using Maven to do this. Using Maven should works fine with beans.xml in src/main/resources/META-INF.

希望这也可以在这种情况下帮助其他人.

Hope this can help others on this situation too.

这篇关于容器未找到.jar内的CDI Bean(不满意的依赖项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆