-
使用spring與struts2整合
- 需要導入spring-web-4.2.4.RELEASE.jar
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springday01_crm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- spring的webfilter,用來啟動servlet時加載applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
service與dao
package service;
import dao.UserDao;
import dao.UserDaoImpl;
public class UserServiceImpl implements UserService{
//使用依賴注入來獲得udao,必須提供setter方法
private UserDaoImpl udao;
public void setUdao(UserDaoImpl udao) {
this.udao = udao;
}
@Override
public void save() {
System.out.println("service層 save");
udao.save();
}
}
package dao;
public class UserDaoImpl implements UserDao {
//dao層方法
@Override
public void save() {
System.out.println("Dao層 save");
}
}
測試類
package action;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionSupport;
import service.UserService;
import service.UserServiceImpl;
public class UserAction extends ActionSupport{
//測試struts環境方法
public String demo1() {
System.out.println("running " + this.getClass().getName() + "." + new Exception().getStackTrace()[0].getMethodName());
return SUCCESS;
}
//跳轉add頁面
public String addUI() {
return "addUI";
}
/*
* #使用struts2調用service層的方法
public String add() {
// System.out.println("running " + this.getClass().getName() + "." + new Exception().getStackTrace()[0].getMethodName());
System.out.println("web層 save");
UserService us = new UserServiceImpl();
us.save();
return NONE;
}
*/
/*
* 使用spring工廠的模式來調用已交給spring管理的bean
* 注意:這樣可以實作,但是每次調用此方法都會加載一次配置檔案新建立一個ApplicationContext,這不是我們想要的
* 解決方法:
* 導入spring核心包中的web jar包用WebApplicationContext解決此問題
* /
*/
// public String add() {
//
// ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// UserService us = (UserService) ac.getBean("userService");
// us.save();
// return NONE;
// }
/*
* 0. 解決發送每通路一次都會加載一次配置檔案的問題
* 1.引入spring-web-4.2.4.RELEASE.jar包
* 2 .配置監聽器
* 3.從ServletContext中獲得工廠繼續業務
*/
public String add() {
//獲得servletContext
ServletContext servletContext = ServletActionContext.getServletContext();
//獲得WebApplicationContext需要傳入一個servletContext
WebApplicationContext wContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
//獲得userService
UserService us = (UserService) wContext.getBean("userService");
//繼續業務
us.save();
return NONE;
}
}
每次都加載配置檔案的結果

使用spring web包之後的效果
web包的原理:
* 将工廠建立好了以後放入到ServletContext域中.使用工廠的時候,從ServletContext中獲得.
* ServletContextListener:用來監聽ServletContext對象的建立和銷毀的監聽器.
* 當ServletContext對象建立的時候:建立工廠 , 将工廠存入到ServletContext