Struts 2 Spring integration: a kickstart on using dependency injection

In this post you will find how we can integrate Spring with Struts2 through struts2-spring-plugin. And we will also see how we can use dependency injection. I assume that you are successfully running a Struts2 application. We will add our Spring integration code in that.

Lets take a scenario. You have an Interface DAO.java that has abstract methods. You have written separate implementation for it. Lets suppose for different databases. So we are going to use the implementation of this DAO in our Struts2 application.

DAO.java

package com.pakzilla;
public interface DAO {
public void save(Object o);
}

DAOOracle.java

package com.pakzilla;
public class DAOOracle implements DAO {

public void save(Object o) {
System.out.println("Oracle implementaion");
}
}

DAOMySQL.java

package com.pakzilla;
public class DAOMySQL implements DAO {

public void save(Object o) {
System.out.println("MySQL implementaion");
}
}

DAOService .java

package com.pakzilla;
public class DAOService {
private DAO dao;

public void setDao(DAO _dao){
this.dao = _dao;
}

public DAO getDao(){
return dao;
}

public void saveRecord(Object o){
dao.save(o);
}
}

applicationContext.xml. You will put this file in JavaSource. So it will become the part of your classpath.


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

<bean id="daoOracle" class="com.pakzilla.DAOOracle" />
<bean id="daoMysql"   class="com.pakzilla.DAOMySQL"/>

<bean id="daoService" class="com.pakzilla.DAOService">
<property name="dao">
<!-- swith the daos here -->
<ref local="daoMysql"/>
</property>
</bean>
</beans>

Add following in web.xml. You see we are using classpath prefix with applicationContext.xml. Because this file will be in our classpaths. And on server startup web.xml will load it.


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Somewhere in your Struts2 Action, where you want to insert the record.

ApplicationContext context =
       new ClassPathXmlApplicationContext("applicationContext.xml");
DAOService daoService = (DAOService) context.getBean("daoService");
daoService.saveRecord(<your object to save>);

You must have following jars in your classpath.

antlr-runtime-3.0.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
junit-3.8.1.jar
ognl-2.6.11.jar
org.springframework.asm-3.0.0.M3.jar
org.springframework.beans-3.0.0.M3.jar
org.springframework.context-3.0.0.M3.jar
org.springframework.core-3.0.0.M3.jar
org.springframework.expression-3.0.0.M3.jar
org.springframework.web-3.0.0.M3.jar
org.springframework.web.servlet-3.0.0.M3.jar
spring.jar
struts2-convention-plugin-2.1.6.jar
struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
xwork-2.1.2.jar
Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *