容器找不到.jar中的CDI bean(不满意的依赖项)

Ren*_*gio 7 java-ee cdi jboss-weld weld

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

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

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

WELD-001408: Unsatisfied dependencies for type Session with qualifiers (...)
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

下面是jar中需要注入其他项目的bean(Session,SessionFactory和ExampleLogger)的示例(并且在类在Web项目中时已正常工作)但现在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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
public class ExampleLoggerProducer {

    @Produces
    public ExampleLogger createLogger(InjectionPoint injectionPoint) {
        return new ExampleLogger(injectionPoint.getMember().getDeclaringClass());
    }
}
Run Code Online (Sandbox Code Playgroud)

问题出现在Maven项目和非Maven项目中.有人遇到过这个问题吗?是否有人知道容器中存在罐中存在的豆子的解决方案?提前谢谢你,抱歉英语不好.

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

Ren*_*gio 5

找到解决方案后再试一次?我将beans.xml放在项目根文件夹内的META-INF文件夹上,它可以正常工作!这与在WELD文档中有关如何在jar中使用CDI bean的描述背道而驰,该地方说放置bean.xml的位置在src / main / resources / META-INF内,但可以。也许在某些Maven项目中,这是真的。

[更新]这行得通,因为我是使用Eclipse的导出jar向导而不是使用Maven来构建jar。使用Maven应该可以与src / main / resources / META-INF中的beans.xml配合使用。

希望这也可以帮助其他人解决这种情况。