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).
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.
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 (...)
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.
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());
}
}
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, server WildFly 8.1
beans.xml
. Does it specifybean-discovery-mode="annotated"
, if so, annotate the producer classes, at least withApplicationScoped
– Honshu