Open In App

Resource Injection in Java EE

Last Updated : 15 May, 2022
Like Article

The concept of resource injection can be used to inject any resource available in the JNDI (Java Naming and Directory Interface) namespace into any container-managed object, such as a servlet, a bean, etc. For example, resource injection can be used to inject data sources, connectors (such as database connectors), or custom resources that are available in the JNDI namespace. JNDI allows the simplification of a resource construct into just a name thereby grouping many details into one for convenience/security/etc. The type used for referring to the injected instance is usually an interface, which decouples your application code from the implementation of the resource. There’s a scenario to explain this concept.


A single database connection will not scale for multiple web users whereas database connection pools allow web apps to scale and handle multiple users quickly. Connection pooling can be achieved as shown below:



    <Resource name="jdbc/test" 

As mentioned earlier, the entire resource is simplified into a name “jdbc/test” and the authentication will be handled by the Tomcat server. Now the resource can be injected into another place where it’s necessary. In this case, it’s injected into a servlet which can be used elsewhere. 

Note: The following code snippet is just the partial Java code for the given servlet, proper syntax has to be followed when writing the servlet class.


import javax.sql.DataSource;
private DataSource ds;

The @Resource annotation, which is in the javax.annotation package is used to inject the resource into the servlet class. Now, “ds” is the keyword to be used for injecting the resource into another place. As an example: (continued)


private StudentDbUtil studentDbUtil = new StudentDbUtil(ds);

Now the control goes to another Java class from the servlet where the resource is injected. 

Note: The following code snippet is just the partial Java code for the given servlet, proper syntax has to be followed when writing the servlet class.


public StudentDbUtil(DataSource theDataSource) {
    private DataSource dataSource = theDataSource;

After this, the “dataSource” keyword is used to establish the connection using the credentials that were defined initially in the context.xml file. The final code snippet completes the explanation of the resource injection concept. (Continued)


// "dataSource" resource is used here
myConn = dataSource.getConnection(); 
// create sql statement
String sql = "select * from students order by studentid";
myStmt = myConn.createStatement();
// execute query
myRs = myStmt.executeQuery(sql);

In short, these are the steps that take place in the entire process:

  1. Credentials for connecting with the database (along with connection pooling) are defined in the context.xml file as a resource assigning a name for the resource.
  2. The resource is injected into the Java servlet using the resource name.
  3. The resource is subsequently passed into the Java class where the database connection is to be established and the connection pool is invoked as and when the resource is invoked.

The above-mentioned example is the use-case of resource injection which provides advantages such as reduced repetition of code, direct injection of JNDI resources into various Java classes, etc.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads