0

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?

Dominique
  • 16,450
  • 15
  • 56
  • 112
Thomas Bayer
  • 71
  • 1
  • 3
  • If you created your BckingBean instance by "new BackingBean()" CDI will not work. Did you? – Rainer Feike Dec 13 '17 at 12:21
  • the backing bean instance is created automatically via JSF-access. – Thomas Bayer Dec 13 '17 at 12:55
  • 4
    You are mixing EJB and CDI quite wildly. Firstly, a CDI bean (`@SessionScoped BackingBean` in your case) doesn't need to be `@ManagedBean`. Secondly, try `@Inject` instead of `@EJB` if you want CDI to do the job (in that `@SessionScoped` bean). – Siliarus Dec 13 '17 at 13:57
  • and could you share the imports for `ManagedBean` and `SessionScoped`? – John Ament Dec 14 '17 at 03:09

2 Answers2

0

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
wrgoff
  • 1
  • 1
-2

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;

}
chkal
  • 5,598
  • 21
  • 26
  • *"In this case @EJB won't work correctly"* this statement is not true. `@EJB` has always worked fine in JSF native `@ManagedBean` for ages. The only what doesn't work is `@Inject`, but OP isn't using that at all. OP's problem is elsewhere not visible in the information provided so far. My guess would be that OP is actually attempting to access the EJB instance inside bean's constructor instead of `@PostConstruct`. Even migrating to CDI won't solve that. – BalusC Dec 22 '17 at 10:06
  • Thanks for the clarification. I wasn't aware of that. – chkal Dec 22 '17 at 10:31