spring学习七 spring和dynamic project进行整合

  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容器。

posted @ 2018-10-02 11:47  阿瞒123  阅读(626)  评论(0编辑  收藏  举报