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> 
  • 相关阅读:
    Java 访问标识符
    Java 类变量与实例变量的区别
    Java 变量
    python install sublime安装
    Failed to resolve com.android.support:support-annotations 26.0.1
    Git的使用及托管代码到GitHub
    Recyclerview点击事件,更新item的UI+更新Recyclerview外的控件
    第一次android混淆实战
    android计算屏幕dp
    显示当前日期时间
  • 原文地址:https://www.cnblogs.com/64Byte/p/13113684.html
Copyright © 2011-2022 走看看