CDI Beans inside . jar file are not found by container (Unsatisfied dependencies)

Asked

Viewed 80 times

2

I created a Java project to lib other projects, decreasing code duplicity between projects. This lib project is exported to jar to be included in Web projects.

In the Web projects (from which these classes are being removed) everything worked normally while all classes were kept in them injection of simple and complex objects (those that have Producers and configurations) worked normally.

After removing these classes from web projects and adding the jar with these same classes to the project (setting this lib in pom.xml in Maven projects) everything is compiled normally, as it was before too. But when starting the server, the classes (CDI Beans) present now in the jar are not found by the container during CDI startup, generating this (famous) error:

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

I’ve already added the Beans.xml in the META-INF folder both in the src/main/Resources directory (indicated in the WELD and CDI documentation) and in the project root folder and in the META-INF folder created in the project root with the MANIFEST.MF file, but the problem persists.

inserir a descrição da imagem aqui

Below is an example of bean in the jar that needs to be injected into other projects (and that normally worked while the class was in the Web projects) but is not being discovered by CDI:

public class HibernateConnectionFactory {

    @Produces
    @ApplicationScoped @ConnectionBaseExemplo
    public SessionFactory produzirSessionFactoryExemplo() {
        Configuration configuration = new Configuration();
        configurarSessionFactory(configuration, "baseExemploDS");
        ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        return configuration.buildSessionFactory(registry);
    }

    @Produces
    @RequestScoped @ConnectionBaseExemplo
    public Session produzirSessionExemplo(@ConnectionBaseExemplo SessionFactory sessionFactory) {
        return sessionFactory.openSession();
    }

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

The problem occurs in both Maven and non-Smaven projects as well. Has anyone ever faced this problem? Know a solution for the Beans present in jar to be found by the container?

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

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.