zoukankan      html  css  js  c++  java
  • Spring与web的整合(xml/纯注解)

     基于xml的web开发

     目的: Spring容器的创建在Tomcat启动的时候,自动创建, 并且把Spring容器保存到Application域

    问题:Servlet对象无法交给Spring管理, Servlet由Tomcat创建管理, Servlet依赖Service层的类, 无法使用Spring的DI,进行注入, 只能手动创建Spring容器,调用getBean(), 每一个Servlet创建一个Spring容器, 造成Spring容器有多个.

      保证Spring容器只需要一个. 而且方便Servlet获取到这个Spring容器,

    创建时机:  在Tomcat启动的时候,创建, 

    JavaWeb三大组件:Servlet, Filter,Listener(监听器)

    保存的地方: 域对象, 存在ServletContext(Application域)

    1、导入spring-web的依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>

    2、在web.xml文件中, 配置创建Spring容器的监听器:

    <!-- 创建Spring容器的监听器: 在Tomcat启动的时候创建 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- spring基于xml文件开发, 这个监听器默认加载spring配置文件: 
              项目名/WEB-INF/applicationContext.xml
              而我们的spring配置文件, 位于WEB-INF/classes/applicationContext.xml
              
              WEB-INF/classes/:   类路径, classpath: 
            
            使用添加名为contextConfigLocation的context-param
         -->
         <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:applicationContext.xml</param-value>
         </context-param>

    3、获取Spring容器,从容器获取对象(servlet中)

    ApplicationContext applicationContext = (ApplicationContext)this.getServletContext().
        getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); IUserService userService
    = applicationContext.getBean(IUserService.class);

    第二种方式,提供了一个帮助类, 获取Spring容器:

    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

    基于纯注解的web开发

    1、不需要applicationContext.xml文件

    2、web.xml中的配置

    <!-- 创建Spring容器的监听器: 在Tomcat启动的时候创建 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    <!-- 基于纯注解方式: -->
        <!-- 告诉ContextLoaderListener创建的Spring容器的类:  
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        -->
        <context-param>
            <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </context-param>
        
        <!--告诉ContextLoaderListener的配置类是哪一个  -->
        <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>spring05.config.SpringConfiguration</param-value>
         </context-param> 
  • 相关阅读:
    不能执行已释放的Script的代码(ie错误)
    javascript数组
    Jquery遍历方法
    Jquery选择器汇总
    使用xmlHttprequest 发送异步请求(Ajax核心对象)
    不使用局部变量和for循环或其它循环打印出如m=19,n=2結果为2 4 8 16 16 8 4 2形式的串
    解决Js跨域访问的问题
    Oracle 第一天
    计算机图形学1——绪论
    数据库
  • 原文地址:https://www.cnblogs.com/64Byte/p/13113684.html
Copyright © 2011-2022 走看看