WildFly 11: CDI does not work

1.1k Views Asked by At

I have written a very simple application, but CDI does not work as expected:

the definition

@EJB private CustomerProviderSessionBeanLocal customerProvider;

does not lead to an instance of the bean.

Definition of my stateless session bean

@Local
public interface CustomerProviderSessionBeanLocal { ... }

@Stateless
@EJB(name="ejb/CustomerProvider", beanInterface = CustomerProviderSessionBeanLocal.class, beanName = "CustomerProviderSessionBean")
public class CustomerProviderSessionBean implements 
CustomerProviderSessionBeanLocal ...

Controller Bean (for JSF):

@SessionScoped @ManagedBean
public class BackingBean {
    @EJB private CustomerProviderSessionBeanLocal customerProvider;

JBoss yields:

java:global/2017_JEE_App_1_war_exploded/CustomerProviderSessionBean!beans.CustomerProviderSessionBeanLocal

java:app/2017_JEE_App_1_war_exploded/CustomerProviderSessionBean!beans.CustomerProviderSessionBeanLocal
java:module/CustomerProviderSessionBean!beans.CustomerProviderSessionBeanLocal
java:global/2017_JEE_App_1_war_exploded/CustomerProviderSessionBean
java:app/2017_JEE_App_1_war_exploded/CustomerProviderSessionBean
java:module/CustomerProviderSessionBean

Still, the attribute customerProvider is not initialized. The constructor has been called (can be seen in the log-file). I have tried several variants (with/without names, local interface, etc.). Using JNDI-Lookup does work:

initialContext = new InitialContext();
Object o = initialContext.lookup("java:app/2017_JEE_App_1_war_exploded/CustomerProviderSessionBean");

Using the same JNDI-Name in the @EJB-annotation does not work

I haven't changed the wildfly configuration!

Can anyone help?

2

There are 2 best solutions below

2
On

It looks like your JSF controller is using the JSF managed bean feature instead of CDI. In this case @EJB won't work correctly. You should declare your class like this:

@javax.inject.Named
@javax.enterprise.context.SessionScoped
public class BackingBean {

    @EJB 
    private CustomerProviderSessionBeanLocal customerProvider;

}
0
On

I got This working with Wildfly 11, and J2EE 7. I added the following to my Global Modules section in the standalone.xml file.

<subsystem xmlns="urn:jboss:domain:ee:4.0"> <global-modules> <module name="javax.enterprise.api" slot="main"/> ... </global-modules> </subsystem>

The other way to make it work without changing the standalone.xml (or domain.xml) file. is to add the following jar to your ear.

  1. cdi-api-1.2.jar