接上文spring集成web环境(手动实现)

##########代码接上文#############

spring提供了一个监听器ContextLoaderListener对上述功能的封装,该监听器内部加载spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获取上下文对象

1.导入spring-web坐标

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

2.在web.xml中配置ContextLoaderListener监听器

<!--    配置监听器,spring-web提供的-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 全局初始化参数,告诉配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

3.使用WebApplicationContextUtils获取上下文对象ApplicationContext

public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ServletContext servletContext = this.getServletContext();
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save1();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

4.启动服务器,访问userServlet
结果:sava running . . .

Spring集成web环境(使用封装好的工具)的更多相关文章

  1. Spring集成web环境(手动实现)

    1.创建UserDao接口及其实现类UserDaoImpl(接口代码省略) public class UserDaoImpl implements UserDao { @Override public ...

  2. Spring与Web环境集成

    1. Spring与Web环境集成 1.1 ApplicationContext应用上下文获取方式 应用上下文对象是通过new ClasspathXmlApplicationContext(sprin ...

  3. Spring(五)Spring与Web环境集成

    MVC 是 Model.View 和 Controller 的缩写,分别代表 Web 应用程序中的 3 种职责. 模型:用于存储数据以及处理用户请求的业务逻辑. 视图:向控制器提交数据,显示模型中的数 ...

  4. Shiro集成web环境[Springboot]-基础使用

    Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...

  5. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

  6. spring集成环境下的axis webservice的发布,调试

    在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...

  7. Web环境中Spring的启动过程

    1.spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置: …… <context-par ...

  8. Spring-IOC 在非 web 环境下优雅关闭容器

    当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContex ...

  9. 11款PHP集成开发环境

    1. Zend Studio Zend Studio 来自 Zend —— PHP 幕后的精英团队. 它真的是很不错.它运行于三大系统:Windows.Mac OS X 和 Linux.而且绝对能够提 ...

随机推荐

  1. JDK 线程池

    JDK 线程池 线程池参数 在JDK的4种线程池之前, 先介绍一下线程池的几个参数 corePoolSize 线程池的核心线程数量, maximumPoolSize 线程池的最大线程数量 keepAl ...

  2. 在线O(1)求逆元

    怎么还有厉害的在线O(1)求逆元,不过常数确实有点儿太大了 本文大部分搬运于这里 相信大家都做过 POJ2478 这道题吧,这道题的 Farey 序列 \(F_n\) 包含了分子分母不大于 \(n\) ...

  3. 在kali中安装两个版本的python

    在kali中安装两个版本的python 在kali中已经预装了两个版本的python,但是没有装pip(>︿<). 这时使用python或者python3表示python3:使用pytho ...

  4. springcloud学习00-开发工具相关准备

    用maven构建springcloud项目,目录结构(图片来源:https://blog.csdn.net/qq_36688143/article/details/82755492) 1.maven ...

  5. mycat的安装及使用 看这一篇就够了

    1.环境准备 ​ 本次使用的虚拟机环境是centos6.5 ​ 首先准备四台虚拟机,安装好mysql,方便后续做读写分离和主从复制. 192.168.85.111 node01 192.168.85. ...

  6. Citus 分布式 PostgreSQL 集群 - SQL Reference(创建和修改分布式表 DDL)

    创建和分布表 要创建分布式表,您需要首先定义表 schema. 为此,您可以使用 CREATE TABLE 语句定义一个表,就像使用常规 PostgreSQL 表一样. CREATE TABLE ht ...

  7. Math.round(11.5)等於多少? Math.round(-11.5)等於多少?

    Math类中提供了三个与取整有关的方法:ceil.floor.round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,所以,Math.ceil( ...

  8. python django对数据表的增删改查操作

    新增操作:方式1:book = BookInfo(title='西游记',price=99)book.save() 方式2:BookInfo.objects.create(title='西游记',pr ...

  9. springcloud断路器的作用?

    当一个服务调用另一个服务由于网络原因或自身原因出现问题,调用者就会等待被调用者的响应 当更多的服务请求到这些资源导致更多的请求等待,发生连锁效应(雪崩效应) 断路器有完全打开状态:一段时间内 达到一定 ...

  10. phptorm 集成git和gitlab和一些命令

    mac: 2.phpstorm->csv->create git... 3.命令:ssh-keygen -t rsa -C"your_email@youremail.com&qu ...