栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

第6章Spring 与 Web(第二个练习,使用监听器)

Java 更新时间:发布时间: 百科书网 趣学号
使用 Spring 的监听器 ContextLoaderListener            对于 Web 应用来说, ServletContext 对象是唯一的,一个 Web 应用,只有一个 ServletContext 对象,该对象是在 Web 应用装载时初始化的。若将 Spring 容器的创建时机, 放在 ServletContext 初始化时,就可以保证 Spring 容器的创建只会执行一次,也就保证了 Spring 容器在整个应用中的唯一性。         当 Spring 容器创建好后,在整个应用的生命周期过程中, Spring 容器应该是随时可以被 访问的。即, Spring 容器应具有全局性。而放入 ServletContext 对象的属性,就具有应用的 全局性。所以,将创建好的 Spring 容器,以属性的形式放入到 ServletContext 的空间中,就 保证了 Spring 容器的全局性。       上述的这些工作,已经被封装在了如下的 Spring 的 Jar 包的相关 API 中:        spring-web-5.2.5.RELEASE 项目结构

 

Step1 : maven 依赖 pom.xml 新增的监听器依赖 < dependency > < groupId > org.springframework < artifactId > spring-web < version > 5.2.5.RELEASE



    
        spring3
        com.it
        1.0-SNAPSHOT
    
    4.0.0

    springWeb
    war

    springWeb Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
        
            org.springframework
            spring-web
            5.2.5.RELEASE
        
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
        
            javax.servlet.jsp
            jsp-api
            2.2.1-b03
            provided
        
        
        
            junit
            junit
            4.11
            test
        
        
        
            org.springframework
            spring-context
            5.2.5.RELEASE
        
        
        
            org.springframework
            spring-tx
            5.2.5.RELEASE
        
        
            org.springframework
            spring-jdbc
            5.2.5.RELEASE
        
        
        
            org.mybatis
            mybatis
            3.5.1
        
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
        
            mysql
            mysql-connector-java
            5.1.9
        
        
        
            com.alibaba
            druid
            1.1.12
        
    

    

        
        
        
            
                src/main/java
                
                    ***.xml
                
                false
            
            
                src/main/resources
                
                    ***.xml
                
                false
            
        


        
        
            
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
        




        
            
                
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-jar-plugin
                    3.0.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
                
                
                    maven-site-plugin
                    3.7.1
                
                
                    maven-project-info-reports-plugin
                    3.0.0
                
            
        
    

Step2 :注册监听器 ContextLoaderListener            若要在 ServletContext 初 始 化 时 创 建 Spring 容 器 , 就 需 要 使 用 监 听 器 接口ServletContextListener 对 ServletContext 进行监听。在 web.xml 中注册该监听器。




    RegisterServlet
    com.it.controller.RegisterServlet


    RegisterServlet
    /RegisterServlet


    

        contextConfigLocation

        classpath:applicationContext.xml
    
    
        
            org.springframework.web.context.ContextLoaderListener
        
    

    
           Spring 为该监听器接口定义了一个实现类 ContextLoaderListener ,完成了两个很重要的 工作:创建容器对象,并将容器对象放入到了 ServletContext 的空间中。           打开 ContextLoaderListener 的源码。看到一共四个方法,两个是构造方法,一个初始化 方法,一个销毁方法。 Step3 :指定 Spring 配置文件的位置       ContextLoaderListener 在对 Spring 容器进行创建时,需要加载 Spring 配置文件。其默认 的 Spring 配置文件位置与名称为: WEB-INF/applicationContext.xml 。但,一般会将该配置文 件放置于项目的 classpath 下,即 src 下,所以需要在 web.xml 中对 Spring 配置文件的位置及 名称进行指定。

 

Step4 :获取 Spring 容器对象 在 Servlet 中获取容器对象的常用方式有两种: ( 1 ) 直接从 ServletContext 中获取         从对监听器 ContextLoaderListener 的源码分析可知,容器对象在 ServletContext 的中存 放的 key 为 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 。所以,可以直接通过 ServletContext 的 getAttribute() 方法,按照指定的 key 将容器对象获取到。

(2) 通过 WebApplicationContextUtils 获取 

        工具类 WebApplicationContextUtils 有一个方法专门用于从 ServletContext 中获取 Spring         容器对象: getRequiredWebApplicationContext(ServletContext sc)

package com.it.controller;

import com.it.domain.Student;
import com.it.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.io.IOException;

public class RegisterServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        request.setCharacterEncoding("utf-8");//表示从客户端页面上获取过来的是utf-8的格式,去掉会产生乱码
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String dept = request.getParameter("dept");

//        方法1.创建spring的容器对象
//        String config="applicationContext.xml";
//        ApplicationContext ac=new ClassPathXmlApplicationContext(config);

        WebApplicationContext wac=null;
//   方法2.获取ServletContext中的容器对象,创建好的容器对象,拿来就用
//        String key=WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
//        Object attribute = getServletContext().getAttribute(key);
//        if (attribute != null){
//            wac= (WebApplicationContext) attribute;
//        }

//        方法3.使用框架中的方法,获取容器对象
        ServletContext sc=getServletContext();
       wac= WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

        System.out.println("容器对象的信息,======="+wac);

//        获取service
        StudentService studentService = (StudentService) wac.getBean("studentService");
        Student student=new Student();
        student.setSno(Integer.parseInt(id));
        student.setSname(name);
        student.setSage(Integer.valueOf(age));
        student.setSsex(sex);
        student.setSdept(dept);
        studentService.addStudent(student);


//       给一个页面
        request.getRequestDispatcher("/result.jsp").forward(request,response);

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request,response);
    }
}
           以上两种方式,无论使用哪种获取容器对象,刷新 success 页面后,可看到代码中使用的 Spring 容器均为同一个对象。

 

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/959811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号