1

We have trouble finding the correct way of injecting a SessionBean from another container.

Lets say we have two ear-files earA and earB and an shared jar:

earA

@Stateless
public class MyBean implements BeanInterface{...}

earB

@Stateless
public class OtherBean{
   @Inject BeanInterface myBean;
}

apiJar

A jarfile containing BeanInterface is present in both earA and earB

We don't want to explicitly use the JNDI name via @EJB, because earB will be implemented and deployed by an external customer.

We want to make as easy as possible (and with as little knowledge of earA as possible) for earB to inject MyBean through the BeanInterface.

mathiasbn
  • 881
  • 11
  • 21
  • Where did you find information that CDI would work across Ears? If in earB you will use @EJB, you will be able to map that via reference to the bean in earA without code changes in earB. – Gas Sep 19 '14 at 08:55
  • Not sure i understand your response. You want me to use the @EJB annotation and then specify the implementing class? Then i force my external customer to know about my implementation details... – mathiasbn Sep 19 '14 at 11:04
  • No. Use standard `@EJB(name="ejb/BeanInterfaceRef") BeanInterface myBean;` Then you can map `ejb/BeanInterfaceRef` to the JNDI of bean in earA, for example via jboss.xml - http://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/ENC_Usage_Conventions-EJB_References_with_jboss.xml_and_jboss_web.xml.html – Gas Sep 19 '14 at 11:22

1 Answers1

1

CDI will not work across EARs.

You can use @EJB(lookup=...) in EAR B to get a reference to the EJB implemented in EAR A.

In this case, you do not need to know the implementing class in EAR A, you only need to know its JNDI name. You should also probably make the EJB in EAR A exposing a remote interface.

If your two EARs are deployed on different server instances, you'll also need some vendor specific configuration to perform the remote JNDI lookup.

You can also hide this @EJB(lookup=...) and use CDI for injecting EJB by doing something described here: http://whoopdicity.blogspot.ro/2014/07/developing-dynamic-jee-service-client.html

Benjamin
  • 1,816
  • 13
  • 21