zoukankan      html  css  js  c++  java
  • 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容器。

  • 相关阅读:
    [NOI2014]起床困难综合症(贪心+位运算)(暑假ACM1 A)
    BZOJ 2456 mode(找众数)(暑假ACM1 F)
    [POI2000]病毒(补全AC自动机)
    [NOI2005]聪聪与可可
    BZOJ4500矩阵
    网络编程物理层
    当列表推导式遇到lambda(匿名函数)
    写学生管理系统后的一些感想
    深入学习python内存管理
    python基础详解
  • 原文地址:https://www.cnblogs.com/cplinux/p/9736708.html
Copyright © 2011-2022 走看看