CDI beans within .jar file are not found by the container (Unsatisfied dependencies)

1

I created a Java project to serve as lib to other projects, reducing duplication of code between projects. This lib project is exported to jar to be included in Web projects.

In Web projects (where these classes are being removed) everything worked normal while all classes were kept in them - the injection of simple and complex objects (those with Producers and configurations) .

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

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

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

Belowisanexampleofabeanpresentinthejarthatneedstobeinjectedintootherprojects(andthatwouldnormallyworkwhiletheclasswasinWebprojects)butisnotbeingdiscoveredbytheCDI:

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 Maven and non-Maven projects as well. Has anyone ever faced this problem? Do you know a solution for the beans in jar to be found by the container?

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

    
asked by anonymous 24.08.2016 / 00:35

0 answers