I am trying to load beans outside a war file. The goal is that is should be possible to change the jar file without a new war file.
My war file includes the external jar as a provided dependency:
<dependency>
<groupId>com.lube</groupId>
<artifactId>foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
I added the jar file into the applibs folder in the domain directory (domain/lib/applibs) and filled out the library field during the deployment.
It seems that no beans are loaded from this jar file (injection is not possible), but it is possible to use the class. (the class is available in the current classloader).
If i am injecting something in a class from the war file i am getting following exception
UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type XXX with qualifiers
I am using payara 4.1.2.174, but it doesn't work with payara 5 too.
The main question is: Should it be possible to load beans (ejb/cdi) outside the war? I don't find any good solution in the web.
P.S.: I can not use microservices ;)
Edit: The war file does not contain the jar file. The jar file is currently located in the domain under /lib/applibs ( I tested all other folders too )
The command how i did that was the following asadmin add-library --type app C:\library.jar
The library.jar contains only one CDI bean
@Model
public class CdiBean {
public void test() {
System.out.println("It works!");
}
}
and the beans.xml file. (resources/META-INF - I tested it without the META-INF folder too)
The WAR file has a startup class which tries to load the Bean.
@Singleton
@Startup
@TransactionManagement(value = javax.ejb.TransactionManagementType.BEAN)
public class AppStartup {
@PostConstruct
public void postConstruct() {
try {
System.out.println(CdiBean.class);
CDI.current().select(CdiBean.class).get().test();
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
The output after the deployment of the war is:
[2020-01-21T13:47:36.413+0100] [Payara 4.1] [INFO] [] [] [tid: _ThreadID=103 _ThreadName=admin-thread-pool::admin-listener(3)] [timeMillis: 1579610856413] [levelValue: 800] [[ class com.lube.CdiBean]]
[2020-01-21T13:47:36.474+0100] [Payara 4.1] [SEVERE] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=103 _ThreadName=admin-thread-pool::admin-listener(3)] [timeMillis: 1579610856474] [levelValue: 1000] [[
Exception while invoking class org.glassfish.ejb.startup.EjbApplication start method
javax.ejb.EJBException: javax.ejb.CreateException: Initialization failed for Singleton AppStartup
...
Caused by: javax.ejb.CreateException: Initialization failed for Singleton AppStartup
at com.sun.ejb.containers.AbstractSingletonContainer.createSingletonEJB(AbstractSingletonContainer.java:553)
at com.sun.ejb.containers.AbstractSingletonContainer.access$000(AbstractSingletonContainer.java:82)
at com.sun.ejb.containers.AbstractSingletonContainer$SingletonContextFactory.create(AbstractSingletonContainer.java:723)
... 72 more
Caused by: java.lang.IllegalStateException: WELD-001334: Unsatisfied dependencies for type CdiBean with qualifiers
... 74 more
Caused by: org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type CdiBean with qualifiers
... 102 more
]]