ClassCastException when injecting a service from WLF fraction to an app
zam0th Jul 5, 2019 10:30 AMSince Thorntail community is non-existing and my posts on their Google Group are eternally on moderation, i post this question here, sorry for that.
I've created a custom TT/WLF fraction (org.wildfly.swarm.mymodule) that declares a service interface (MyService) in "api" slot and a service implementation (MyServiceImpl) that is @ApplicationScoped. I then create a simple app that i package as an uberjar and run on a TT instance that has my custom fraction in it. When i try to use CDI and @Inject MyService into a bean in this app i get this exception:
java.lang.ClassCastException: class MyServiceImpl$Proxy$_$$_WeldClientProxy cannot be cast to class org.jboss.weld.bean.proxy.ProxyObject (MyServiceImpl$Proxy$_$$_WeldClientProxy is in unnamed module of loader 'org.wildfly.swarm.mymodule:api@1-SNAPSHOT' @61001b64; org.jboss.weld.bean.proxy.ProxyObject is in unnamed module of loader 'org.jboss.weld.core@3.0.5.Final' @63ddc753)
If i comment the @Inject out, everything works fine, so i assume that my fraction and app are both configured and instantiated correctly. I'm familiar with proxying, but not with how Weld does it and what happens when you inject a proxy between [it seems] different modules, so I'm completely lost at this point, and i'd appreciate any insight.
This is my bean in the app
@ApplicationScoped public class MyBean { @Inject private MyService service; public void init(@Observes @Initialized(ApplicationScoped.class) Object init) { System.out.println("INIT"); } @PostConstruct public void run() { System.out.println("I'M ALIVE!"); System.out.println(service.getString()); } }
And this is the service in my fraction
@ApplicationScoped public class MyServiceImpl implements MyService { private String stuff = "stuff"; @PostConstruct public void initialize() { System.out.println("INITIALIZED"); } @Override public String getString() { return this.stuff; } }
beans.xml in both of them is essentially the same:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="annotated"> </beans>