0
votes

I have a @Stateless bean in an EJB module

@Stateless 
public class Foo {
    @Inject Bar bar;

    public void helloFromBar() { return bar.hello(); }
}

I have a @Named bean in the same EJB module

@Named @ApplicationScoped
public class Bar {
    public String hello () { return "hello"; }
}

Bar#hello is called from a CDI bean in a web module.

@Named @ViewScoped
public class ViewBean {

    @EJB Foo foo;

    public String callFoo() { return foo.helloFromBar(); }

}

For some reason Bar is not injected into Foo - it is always a null reference. I would expect that this would work.

1
Do you have the constructor of Bar annotated with @Inject? - Keerthivasan
@KeerthiRamanathan Why would that matter? - Brett Kail
@anger Does the EJB module contain META-INF/beans.xml? - Brett Kail
@bkail AFAIK, user defined class should have an injectable constructor? Is that not needed? - Keerthivasan
@KeerthiRamanathan No, they're not needed. Injectable constructors are just an alternative to @Inject fields. I believe the major advantage is the ability to use final fields. - Brett Kail

1 Answers

1
votes

As per @bkail's comment, I was missing META-INF/beans.xml.