spring和web項目進行整合,其實就是在項目啓動時,就創建spring容器,然後在servlet中使用spring容器進行開。
注意:為了頁面可以訪問到servlet,因此servlet必須放進tomcat或者類似的服務器容器中,如果把servlet放進spring容器中,前端頁面是無法訪問的
第一步:導入spring-web.jar包,因為有一些別的依賴關係,還需要導入spring-tx.jar,spring-aop.jar等包
第二步:編寫web.xml配置文件
在web.xml配置一個ServletContext監聽器,在項目一啓動就執行該監聽器,該監聽就會執行創建spring容器的代碼,這樣就可以保證在web項目啓動時就有spring容器。
創建spring容器是需要配置文件的,因此要告訴監聽器到哪裏加載配置文件,加載配置文件的位置通過<context-param>進行配置。
<!-- 配置spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--
spring中配置了一個監聽器
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
類org.springframework.web.context.ContextLoaderListener在spring-web.jar包中
源碼如下,可以看到ContextLoaderListener 是實現了SerlvetContextListener接口的
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
/**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
第三步:在web使用spring容器
@WebServlet("/airportServlet")
public class AirportServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private AirportService airportService;
@Override
public void init() throws ServletException {
WebApplicationContext wa=null;
WebApplicationContext ac=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
airportService=ac.getBean("airportService",AirportServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
List<Airport> airportList = airportService.showAirport();
for (Airport airport : airportList) {
System.out.println(airport);
}
}
}
過程描述:
web項目啓動時,ContextLoaderListener開始執行,ContextLoaderListener監聽器會根據下面的配置讀取spring的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
然後根據配置文件內容創建一個WebApplicationContext類型的容器,這個容器是ApplicationContext的子接口,源碼如下:
public interface WebApplicationContext extends ApplicationContext {
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
String SCOPE_REQUEST = "request";
String SCOPE_SESSION = "session";
String SCOPE_GLOBAL_SESSION = "globalSession";
String SCOPE_APPLICATION = "application";
String SERVLET_CONTEXT_BEAN_NAME = "servletContext";
String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";
String CONTEXT_ATTRIBUTES_BEAN_NAME = "contextAttributes";
ServletContext getServletContext();
}
最後可以在servlet中使用一個WebApplicationContextUtils的工具類獲取WebApplicationContext容器,然後使用spring容器。