Question

I would like to automatically find and inject EJB instances from an EJB container to a List. Example:

@EJBs
List<MyCommonInterface> beans;

or

List<MyCommonInterface> beans;

public MyClass() {
    beans = (List<MyCommonInterface>) context.findBeansByInterface(MyCommonInterface.class);
}

Is this possible?

Was it helpful?

Solution

With JEE6 you can use Instance http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/Instance.html

With that you use an injected context like

@Inject
private Instance<ICommon> ejbs;

and you can iterate over it:

    for(ICommon c : ejbs)
        c.doSomeThing();

here with an example iface like that

  public interface ICommon {
      void doSomeThing();   
  }

I tested it with SLSB and SFSB in an @Singleton.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top