1 year ago

#363687

test-img

j.a

CDI - inject a bean from dependency into class in module

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>

java

jakarta-ee

wildfly

cdi

weld

0 Answers

Your Answer

Accepted video resources