Leonardo
Leonardo

Reputation: 9857

@EJB lookup as parameter instead of hard coded

I have inherited a Java EE web app with a group of JSF that made use of EJB injection for accessing a @Remote interface:

@EJB(lookup = "java:global/myapp-business/myapp-services/CustomerFacadeBean")

It works, but as I am reviewing the application for the deployment guys I would like to make this as a parameter.

I suppose there must be a good reason for the developer to have used only lookup so as a first tentative I am willing to replicate the same scenario.

But, I would like to have opinion to make this injection as much 'portable' and 'configurable' as possible. If I am not wrong in the old days of Ejb 2 there were the ejb-ref in the web.xml.

Upvotes: 0

Views: 499

Answers (2)

arjun99
arjun99

Reputation: 358

From J2EE 6+ lookup attribute is required for JNDI lookup to resolve the target. The other ways of JNDI lookups used in older versions are like for example : @EJB(name="myBean") creates a reference java:comp/env/myBean in component namespace. And if name is not specified in the attribute then it creates a reference as java:comp/env/com.example.MyClass/myField

Upvotes: 0

Brett Kail
Brett Kail

Reputation: 33956

The @EJB declares an EJB references, and application servers are required to support configurable bindings for EJB references at deployment time. The lookup attribute of the annotation allows the developer to specify a default binding unless the deployer overrides it. You will need to consult your application server documentation to determine how to configure this binding.

Note, @EJB annotation has a name attribute, which is what the deployer must configure. If the name attribute is not supplied, then it defaults to <class>/<member>. For example, if the class is test.MyBean and the field is myEjb, then the EJB reference name will be test.MyBean/myEjb.

Upvotes: 1

Related Questions