Resource injection explained

In the JNDI namespace there can be several different types of resources available. These can include Enterprise Java Beans (EJB’s) located in another module, data sources or JMS queues etc.
Earlier those resources were most commonly fetched by doing an JNDI-Lookup in the current environment (the application server).

With resource injection this has been made alot easier by adding an annotation that does the injection for us, instead of having to write the JNDI-lookup code.
The resource injection annotation can thus be applied to any resource whatsoever contained in the JNDI tree, and through this the resource becomes injected to the variable following the annotation when the code is executed. In fact, a resource injection can be made both to a field (variable) but also to a method in form of a parameter to that method. There are however a few rules that the method has to follow:

      - It’s name must start with ‘set’.


      - It must contain only one parameter.


    - It must have a ‘void’ return type.

Below is two small examples of how to use resource injection, both for injecting to a field and a method. In this case, it is a data source that will be injected into a servlet:

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?