Integrate any J2EE-based web application with Spring
Dec 3rd, 2009 | By admin | Category: SpringSpring is a very good modular framework. Each module of the framework can be used depending on the various need in an application. Lets take an example of a J2EE-based web application which can use Spring-DAO module for it’s data base layer or Spring-MVC to make the navigation between Model-View-Controller more modular and testable.
These factors decides which component of the Spring can be used with the J2EE-based web application. If you are using Spring-MVC for your web application , Spring provides a front-controller in form of DispatcherServlet which does loading of of application context , instantiating your beans etc.
But , there might be a situation where Spring-MVC is not your need , rather you would like to take advantage of Spring-DAO module along with normal servlets and jsps. In this scenario , DispatchServlet(the front-controller) is not at all there in picture. But the challenge remain the same. Your application context must be loaded and all the beans should be instantiated.
This post talks on integrating the Spring framework with a J2EE-based web application which has normal servlets and jsps.
Two areas of your web application should be modified to integrate with Spring framework.
- web.xml file – Where you need to specify the path of the Spring’s application context file and register some listeners.
- Your servlet – where you need make changes to get the instance of your bean to call bean’s methods.
Lets assume , we have an application context which declares the beans for configuring and connecting to the Database. The application context file will look like this ,
Listing 1 : Application Context Xml – databaseAppConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/library"/>
<property name="username" value="root"/>
<property name="password" value="novell"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="bookDao" class="com.etechguide.library.client.dao.BookDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
Next , add these tags to the top of web.xml
A context-param tag with the param-name contextConfigLocation and it’s value. contextConfigLocation is the location of Spring’s application context file. you can define multiple application context files and declare them in space separated manner.
<!-- context-param --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/databaseAppConfig.xml</param-value> </context-param>
A listener which would load all these context files and pass it to Spring framework for instantiating the beans defined in the application conext xml file.
<!-- listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Once you are done with this step , use your servlet to get the instance of the bean created for you. If you look closely to the databaseAppConfig.xml file in the Listing 1 , you have a bean named bookDao which is the bean for the class file , com.etechguide.library.client.dao.BookDaoImpl. Lets assume , BookDaoImpl implements the interface BookDao.java and has definition of the methods like getBookDetails(), updateBook() deleteBook() etc. Hence you have to get the instance of BookDaoImpl if you would like to call the methods.
Have look at the following code ,
public class BookDetailsSrvlet extends
javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
private ServletConfig servletConfig = null;
private ServletContext servletContext = null;
private String state = null;
private BookDao bookDao = null;
protected ApplicationContext applicationContext;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.servletConfig = config;
servletContext = this.servletConfig.getServletContext();
if (applicationContext == null) {
applicationContext = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
}
bookDao = (BookDao) applicationContext.getBean("bookDao");
}
.....
.....
// Other codes
.....
.....
}
The above code is from a servlet which makes use of the servletContext to get the ApplicationContext object and then called the getBean() method passing the bean name to get the instance of BookDaoImpl which is a type of BookDao interface.
