Jay SenSharma

Jay SenSharma

Few More Demos Here: (Binding/Accessing JNDI names Using Spring Framework)

Here is a very simple example which demonstrates us …How to lookup in the JNDI tree of WebLogic Server 10.3 using the Spring Framework. To run this example you need to download at least two JARs “Spring.jar” and “spring-jdbc.jar”. The best way to get these JARs is to browse to Spring framework website. But I have a very simple way for you to get these jars …please refer to http://findjar.com/ and just specify the JAR file name there  or if you remember any of the class name which is there inside these JAR then that will also work for you.

Once you have done with downloading with these JARs then start writing the Spring Bean class.

Step 1).  Writing the Spring Bean class.

package pack;
import java.io.Serializable;
public class EmployeeBean implements Serializable
private static final long serialVersionUID = 1L;
private int empNo;
private String empName;

public EmployeeBean()
System.out.println(“nt EmployeeBean Object Instantiated”);

public void init()
System.out.println(“nt init() of EmployeeBean”);

public int getEmpNo() {
return empNo;

public void setEmpNo(int empNo) {
this.empNo = empNo;

public String getEmpName() {
return empName;

public void setEmpName(String empName) {
this.empName = empName;

public String toString() {
return “n<BR>*********************n<BR>Emp Name: ” + empName + “n<BR>Emp Num: ” + empNo  ;

Step 2). Now we need to write the Spring Configuration XML file, here we need to define the Bean information’s like following

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “<a href="http://www.springframework.org/dtd/spring-beans.dtd">http://www.springframework.org/dtd/spring-beans.dtd</a>“>

<bean id=”employee”  init-method=”init”>
<property name=”empNo” value=”1000″/>
<property name=”empName” value=”Jack”/>

<bean id=”dataSource”>
<property name=”jndiName”>

Step3). Now we need to provide the mapping of the Spring Configuration file into the web.xml file

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>” xmlns=”<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>” xmlns:web=”<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>” xsi:schemaLocation=”<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a> <a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>” id=”WebApp_ID” version=”2.5″>





Step 4). No we need to create a simple Servlet using Spring APIs to perform a JNDI lookup on the Server for the a DataSource which has the JNDI Name as “jdbc/MyDS” Object.

package servlets;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.net.URL;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class MyJNDIServlet extends HttpServlet
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
PrintWriter out=res.getWriter();
URL url=Thread.currentThread().getContextClassLoader().getResource(“applicationContext.xml”);
System.out.println(“nnt URL: “+url);
String file=url.getFile();
System.out.println(“nnt FILE: “+file);

BeanFactory factory = new XmlBeanFactory(new FileSystemResource(file));
javax.sql.DataSource bean=( javax.sql.DataSource)factory.getBean(“dataSource”);
out.println(“<h3>org.springframework.jndi.JndiObjectFactoryBean (dataSource) Bean: “+bean);
System.out.println(“ntorg.springframework.jndi.JndiObjectFactoryBean (DataSource)Bean: “+bean);
out.println(“<h4>GOT the CONNECTION :=> ds.getConnection() : “+bean.getConnection());
//System.out.println(“ntds.getConnection() : “+bean.getConnection());

catch (Exception e)

Step 5). Compile the “EmployeeBean.java” and “MyJNDIServlet.java” classes by adding the Spring.jar and Spring-jdbc.jar files in the CLASSPATH of the command prompt.

javac    -d  .  MYJNDIServlet.java

javac    -d  .  EmployeeBean.java

Step 6). Now place the compiled classes and the Spring Configuration “applicationContext.xml” file inside the “WEB-INF/classes” directory.

Step 7).  Place the “spring.jar” and “spring-jdbc.jar” files inside the “WEB-INF/lib” directory.

Step 8). Write a Simple “index.jsp” page with a link to your Servlet…

<%@ page language=”java” contentType=”text/html;charset=UTF-8″%>
<a href=”MyServlet”>Click Here To Test Simple SpringBean.</a>
<a href=”MyJNDIServlet”>Click Here To Test <B><i>JNDI Spring Bean</B></i>.</a>

Step 9). Create a WAR File of above contents and then before deploying it on the Server. Finally Create a DataSource on the Server with the JNDI Name “jdbc/MyDS”.

Step 10). Deploy the Application On the Server and then access the MyJNDIServlet to perform a JNDI lookup using Spring Framework.


Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.