1

I have the following UserService bean, part of module A:

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class UserService {
    ...
}

I want to programatically inject UserService in another class named LoginHandler. LoginHandler is part of a separate module B that has a dependency on module A.

public class LoginHandler {
    @Inject
    UserService userService;

    public LoginHandler(){
        // programmatic injection
        InitialContext initialContext = new InitialContext();
        Object lookup = initialContext.lookup("java:comp/BeanManager");
        BeanManager beanManager = (BeanManager) lookup;
        AnnotatedType annotatedType = beanManager.createAnnotatedType(LoginHandler.class);
        InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
        CreationalContext creationalContext = beanManager.createCreationalContext(null);
        injectionTarget.inject(this, creationalContext);
        creationalContext.release();
    }
}

However I get a WELD-001408: Unsatisfied dependencies for type UserService with qualifiers @Default at injection point [BackedAnnotatedField] @Inject my.package.LoginHandler.userService error.

It is worth mentioning that I build a jar for module B and its dependencies, including module A (using maven-assembly-plugin). The jar is then installed as a WildFly module, where the error is caused at runtime when construcing a LoginHandler object.

Anyone who can help?

Update:

My module.xml looks like this:

<module xmlns="urn:jboss:module:1.1" name="my.package.moduleB">
    <resources>
        <resource-root path="module-B.jar"/>
    </resources>
    <dependencies>
        <module name="org.wildfly.security.elytron"/>
        <module name="org.slf4j"/>
        <module name="javax.enterprise.api"/>
        <module name="javax.servlet.api"/>
    </dependencies>
</module>
j.a
  • 31
  • 5
  • It seems odd to install an EJB as a module. TBH I'm not even sure it would work. We'd need to see what your `moudle.xml` looks like though. – James R. Perkins Apr 01 '22 at 16:35
  • @JamesR.Perkins I have added my `module.xml` to the post – j.a Apr 04 '22 at 07:12
  • Again, I don't really know if EJB's work in a module like this, but how do you define the module on your deployment? – James R. Perkins Apr 04 '22 at 16:20
  • @JamesR.Perkins The B module has a dependency on the A module. I build a B jar that contains the classes of module A as well (using maven-assembly-plugin). I then add the B-jar as a WildFly module using the jboss-cli, which generates the `module.xml` mentioned above. – j.a Apr 05 '22 at 08:08
  • In the meantime I worked around the issue by duplicating the code of my `UserService` to module B itself (so it no longer needs a dependency on A) and by avoiding CDI. – j.a Apr 05 '22 at 08:11
  • Do you have a `my.package.moduleA` too then? If so you're missing the dependency in `my.package.moduleB`. – James R. Perkins Apr 05 '22 at 15:19

0 Answers0