Jernej Jerin
Jernej Jerin

Reputation: 3399

Inject Application scoped CDI bean into stateless EJB bean

First I will describe what I am trying to accomplish here. I have a Java application that periodically reads data and calls stateless EJB on JBoss AS 7.1.1 for further operations (computing data and saving it into the DB). Then I have front end which uses JSF 2.0. In controller which is a @ApplicationScoped CDI bean and resides in JSF project I inject EJB into it. Now I need EJB for getting data from the DB. I am using @ApplicationScoped CDI bean because of this reason:

The application context is shared between all servlet requests, web service invocations, EJB remote method invocations, EJB asynchronous method invocations, EJB timeouts and message deliveries to message-driven beans that execute within the same application. The application context is destroyed when the application is shut down.

I want only one CDI bean for all the clients, because the data is independent of the user.

Now I want to update data in @ApplicationScoped CDI bean, which is defined under JSF project with the help of EJB bean, which method is executed when new data arrives. I have already successfully used @Inject in @ApplicationScoped CDI bean, where I have injected EJB from EJB project. Now I want to do the other way around. I tried to inject ApplicationScoped CDI bean from JSF project into EJB bean. But when I wrote this I got @Inject underline as warning:

@Inject
private CurrentDataController currentDataController;

The warning is:

No bean is eligible for injection to the injection point [JSR-299 §5.2.1]

When I try to publish project I get error about class not found exception for CurrentDataController.

Caused by: java.lang.ClassNotFoundException: controllers.CurrentDataController from [Module "deployment.TestEAR.ear.TestEJB.jar:main" from Service Module Loader]

It seems that the EJB project can't reference class in JSF project. Also it is looking for CurentDataController class in TestEJB.jar instead of in the TestJSF.jar. What I am missing here?

The structure of my whole project is as follow:

Now I reckon that after I will fix the error about no class definition found I will have another problem connected with the warning I have posted.

Upvotes: 1

Views: 3852

Answers (1)

Antoine Sabot-Durand
Antoine Sabot-Durand

Reputation: 4970

In an EAR the EJB modules (jar) don't have visibility on the Web modules (war). EJB module is UI agnostic so it's normal to have this layer decoupling. To resolve you issue you have two options.

  1. Move the bean that need to be injected in your EJB in one EJB module (the war would also be able to inject that bean)
  2. Refactor your app to have only a war (yes you can have EJB in war now). Architecture would be simplier and full EJB will work under JBoss (remote EJB too)

Upvotes: 4

Related Questions