Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB cannot inject other EJB

I do have a EJB ActionService which I can inject into other EJBs, that is working fine.

Now I created another EJB:

@Stateless
public class ActionsPerDateDataSet extends ScriptedDataSetEventAdapter  {       
    @EJB
    ActionService actionService;

@Override
public void open(IDataSetInstance dataSet) {
        actionService.foo() // However actionService is null here!
    }
}

Where the ScriptedDataSetEventAdapter comes from another framework (BIRT). However now my actionService is always null. I can not understand why

like image 504
matthias Avatar asked May 14 '26 23:05

matthias


2 Answers

It is possible that the class ScriptedDataSetEventAdapter can not be initialized in the EJB Container (First part of the cycle) and as the initialization is not correct, the dependency injection (@EJB and @Inject) is not made.

What you could do is change the Design of your EJB and instead it's extends "ScriptedDataSetEventAdapter" change it to a composition.

@Stateless
public class ActionsPerDateDataSet   {       

    ScriptedDataSetEventAdapter scriptedDataSetEventAdapter;

    @EJB
    ActionService actionService;

    @PostConstruct
    public void init (){
        try {
            scriptedDataSetEventAdapter = new ScriptedDataSetEventAdapter();
        } catch( AppException e){
        }
    }

@Override
public void open(IDataSetInstance dataSet) {
        actionService.foo() // However actionService is null here!
    }
}
like image 68
JE Zamora Avatar answered May 17 '26 13:05

JE Zamora


You should introduce the lib as ejbModule in ear file , so that container search the jar file and deploy it and inject it whenever it needs

like image 24
Vahid Vakily Avatar answered May 17 '26 12:05

Vahid Vakily