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
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] 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With