Spring和Servlet的整合

使用Spring Boot注册整合方式整合Servlet三大组件 出现这种情况的主要原因是:嵌入式Servlet容器对Servlet、Filter、Listener组件进行了自动化识别和配置,而自定义的Servlet、Filter、Listener都继承/实现了对应的类/接口,同时自定义的Servlet、Filter、Listener组件都使用了@Component注解,这些组件会被自动扫描为Spring组件。打开之前创建的Servlet组件配置类ServletConfig,将该自定义Listener类使用组件注册方式进行注册,示例代码如下。... 阅读详情


ApplicationContext是Spring的核心,是”应用的容器”,在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext;WebApplicationContext的初始化方式和BeanFactory.ApplicationContext有所区别,因为WebApplicationContext需要ServletContext实例,也就是说它必须拥有Web容器的前提下才能完成启动的工作。

spring提供了用于启动WebApplicationContext的监听器:org.springframework.web.context.ContextLoaderListener,使用它可以在web应用启动的时候来初始化WebApplicationContext。如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。 

在Web.xml中配置如下内容:
<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>

如果Spring框架采用B/S系统,通过ServletContext对象获取ApplicationContext对象,然后再通过它获取需要的类实例,常用的两种方法如下:

1、WebApplicationContextwac =  

         WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext())。

2、WebApplicationContextwac =

                       WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

方法1:在Servlet在init()方法中从Spring容器中获取bean

public class SpringServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private WebApplicationContext wac;

    StuBizImpl biz;

    public void doGet(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

         biz.show();

    }

    public void doPost(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

               doGet(request, response);

    }

    public void init() throws ServletException { 

           wac=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

            biz=(StuBizImpl)wac.getBean("stuBizImpl");

    }

}

上面的方法虽然简单,但是很显然,Servlet自身没有在Spring的容器的管理之内。

方法二、编写Servlet的代理bean

  1. 被代理的Servlet

 package cn.com.bochy.servlet;

import java.io.IOException;

import javax.annotation.Resource;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.context.WebApplicationContext;

import cn.com.bochy.biz.StuBizImpl;

@Controller(value="SpringServletProxy")

public class SpringServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private WebApplicationContext wac;

    @Resource

    private StuBizImpl biz;

    public void setBiz(StuBizImpl biz) {

       this.biz = biz;

    }

    public void doGet(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

         biz.show();

    }

    public void doPost(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

               doGet(request, response);

    }

}

  1. 代理的Servlet

 package cn.com.bochy.servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import org.springframework.web.context.WebApplicationContext;

importorg.springframework.web.context.support.WebApplicationContextUtils;

public class ServletProxy extends GenericServlet {

    private Servlet proxy;

    private String servletName;

    private static final long serialVersionUID = 1L;

    @Override

    public void init() throws ServletException {

       super.init();

       WebApplicationContext wac = WebApplicationContextUtils

              .getRequiredWebApplicationContext(this.getServletContext());

       servletName = getServletName();

       System.out.println(servletName);// 这里就是web.xml中的<servlet-name>中的属性值,被代理的Servlet的名字和这个属性值保持一致

       proxy = (Servlet) wac.getBean(servletName);

    }

    @Override

    public void service(ServletRequest arg0, ServletResponse arg1)

           throws ServletException, IOException {

       proxy.service(arg0, arg1);

    }

}

  1. Web.xml中的配置信息

 <servlet-name>SpringServletProxy</servlet-name>

    <servlet-class>cn.com.bochy.servlet.ServletProxy</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>SpringServletProxy</servlet-name>

    <url-pattern>/ServletProxy</url-pattern>

  </servlet-mapping>

<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>

 

spring 与 servlet整合 立即下载

相关推荐

【python】geopandas库安装出现各种问题?可以看看这篇文章

geopandas的安装方法

jgsecurity的博客 3890

spring 整合 servlet

目的:记录spring整合 servlet过程demo。(企业实际开发中可能很少用到),融会贯通。 前言:在学习spring 过程(核心 ioc,aop,插一句 学了spring 才对这个有深刻概念, 在net时候都是直接使用,不得不说 java 还是深刻点)过程中,我们基本上都是在test中测试如图 环境:IDEA 但是,开发中是 spring容器 是共享...

weixin_30662011的博客 236

AD20/Altium designer——如何进行DRC检查、冲突的错误如何改正

对于一个画完的PCB,我们常常需要进行DRC检查,确保板子的电器连接及制作工艺在设定规则的范围内,本篇将介绍如何对PCB进行后期DRC检查处理,确保电路板出现不必要错误。 1.DRC检查入口 2.DRC设置 3.错误分析 对于错误的内容,依据个人实际情况不同,其出现的原因都是因为与设计规则中的设定标准冲突,这里按遇到的错误进行修改讲解,其他错误同理。 4.常见错误分析及解决方法 (1) 解决方法:将未连接网络连接(...

嵌入式创客工坊的博客 5万+

学习笔记(Spring底层+Spring与Servlet整合)

传统的Servlet开发方式的不足 对同一个类型的业务要进行多个Servlet的编写 将业务申请放在一个servlet中(DispatcherServlet)接收所有请求,最后进行分发。 对象管理不方便 将对象的创建和属性的注入存放在xml文件中,对xml文件中的bean和类进行匹配,放入Map中。 对需要创建的类上添加上注解,然后通过注解对属性进行匹配放入Map中 Code(列举几个主要类) DispatcherServlet.java /** * 1.接收客户端的请求 * 2.分发请求 *

LVYX109 397

网卡Inter (R) Wi-Fi 6 AX201突然故障 错误代码10,无法找到无线网络,蓝牙也无效了

问题描述 说来也是坑,简直就莫名其妙。早上在工位上,电脑一切正常(一直用的是无线网络),然后周五准备开周会了,所以拔掉电脑外接设备(电源线,鼠标,外接显示器)拿起电脑到会议室。到了会议室之后,无线网断开了(但一般是不会断开的,都在公司范围内),所以选择重连,但发现已经没有无线网络列表了。 电脑机型:联想YOGA 14s 2021 初步尝试 1、关闭开始飞行模式 - 无果 2、重启并重启 (因为前两天已经提醒过有windows更新,趁这个机会就更新并重启了)- 无果 3、下载鲁大师、驱动精灵、联想驱动管

疯狂的蜗牛 5万+

如何实现C++动态调用动态链接库(DLL或SO)

使用函数指针来定义函数的类型,以便在动态库中找到的函数能够正确地调用。函数指针的类型必须与函数的签名(参数类型和返回类型)匹配。// 函数指针#ifdef _MSC_VER // Windows环境下的导出声明__declspec。

weixin_48438353的博客 7984

spring和servlet之间得关系

servlet、filter和listener是javaweb得三大组成部分。 其中servlet通常是由web.xml统一管理。 而springmvc核心是disparcherservlet为入口。 spring可以管理servlet和springmvc中得javabean,这个意思就是说servlet和springmvc是相对独立的两个servlet, 当你想要在一个继承servlet的实现类中,控制url映射的时候,必须在web.xml配置对应关系,即使你把这个servlet交给spring去

lixiaoyi01的博客 8363

Minitab田口设计实战:如何用正交试验优化遗传算法参数(附完整操作步骤)

本文详细介绍了如何利用Minitab田口设计进行遗传算法参数优化,通过正交试验方法显著提升效率。文章涵盖实验设计、Minitab操作步骤、结果分析及高级技巧,帮助工程师快速掌握这一工业级优化技术,实现算法性能的显著提升。

wind6的博客 866

Spring系列笔记五——Spring整合Servlet

Spring 整合 Servlet 创建业务层 public interface UsersService { void addUsers(); } @Service public class UsersServiceImpl implements UsersService { @Autowired private UsersDao usersDao; @Override public void addUsers() { this.usersDao.insertUsers();

qq_53226437的博客 1036

【遥感物候】植被物候与气候(气温和降水)条件的空间相关性分析

植被生长与气候的关系最为密切,通过计算植被各个生长季参数和气温、降水之间的相关系数可以分析生长季参数的变化与气温、降水之间的关系的程度。本文计算30年的植被物候参数和气候数据之间的相关性,最终效果如下: 一、相关性分析原理 植被生长季及其变化是其生长环境中多个影响因子综合作用的耦合过程,其中气候是最重要、最活跃的环境因子。通过计算各个生长季参数和气温、降水之间的相关系数...

智慧空间实验室 8262

2026华为OD机试(JAVA&Go)真题【E卷+A卷+B卷+C卷+D卷】目录+在线OJ

华为OD机试真题题库【E卷+A卷+B卷+C卷+D卷】目录,Java实现。

MISAYAONE的博客 34万+

spring整合servlet

title: spring整合servlet date: 2019-07-06 14:18:19 tags: spring整合servlet categories: spring 不懂可以给我留言,虽然我也很菜 web整合servlet+spring 整合步骤: 导包,除了导入spring需要的包以外,还要新导入一个: spring-web. 我等会把包都放这里: 所有jar包,...

fanfan999的博客 3239

C/C++炫酷烟花①(完整代码)

C/C++实现炫酷烟花动画的完整代码。

AIGC 2万+

Spring整合Servlet

在实际开发中我们需要用到Spring和Servlet这样的组合(比如写一个小项目啥的)下面我来整理一下我我的整合: 第一步:在web.xml中注册Spring的监听器 这时我们在Web项目中添加Spring的第一步(当然肯定要添加Spring所需要的Jar包啦) <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j

码龙008 2万+

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Spring 如何整合 Servlet 的详细讲解

文章目录Spring 整合 Servlet一、 Jar 包依赖二、 搭建环境1、创建 Web 项目2、添加 Spring 配置文件3、 在 Web 项目中启动 Spring 框架4、 在 Servlet 中获取 Bean 对象创建业务层创建 Servlet创建持久层修改 Spring 配置文件 Spring 整合 Servlet 一、 Jar 包依赖 Spring 核心容器模块: spring-beans-5.2.7.RELEASE.jar spring-context-5.2.7.RELEASE.jar

康康要加油的博客 4704

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

spring整合mybatis和Servlet

Spring+MyBatis+ServletSpring和MyBatis整合Spring和Servlet整合 Spring和MyBatis整合 实现原理:就是让springIOC容器帮我们管理MyBatis框架要使用的对象 导入springIOC和AOP模块、MyBatis框架、c3p0、mybatis-spring相关jar包; 开启注解扫描; 配置要连接数据库的资源文件; 获取连接池信息; ...

leileizheng的博客 687

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Spring Boot 整合 Servlet

Spring Boot 整合 Servlet1.相关依赖2.启动类3.控制类 1.相关依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache

赵大土的博客 230

Spring Boot整合Servlet、Filter和Listener

整合Servlet import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet...

奋斗中的老青年 315

Spring Boot整合Servlet

一、Servlet的配置文件式开发 1.创建一个web项目(项目名称为demo),并编写一个Servlet类。例如:FirstServlet extends HttpServlet 2.需要在web.xml中加入以下配置,以便servlet被web容器管理,这里的web容器为tomcat &lt;servlet&gt; &lt;servlet-name&gt;FirstServlet...

JOFD的博客 527

Spring框架学习笔记11-Spring整合Servlet

1 jar包依赖 2 搭建环境 项目创建 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/

White 496

java spring servlet,spring整合servlet解决方案

当前位置:我的异常网» J2EE»spring整合servlet解决方案spring整合servlet解决方案www.myexceptions.net网友分享于:2013-03-26浏览:232次spring整合servlet在web.xml中添加(导入spring 3的包,不存在包没导入问题)contextConfigLocationclasspath*:applicationCon...

weixin_29002209的博客 222

Spring+Mybatis+servlet的整合

问问

qq_43907358的博客 1018

17 - Spring 学习(11)- web servlet 整合 spring

1 servlet 整合 spring 新建项目 service package com.tzb.service; public interface IUserService { public void add(String username); } package com.tzb.service.impl; import com.tzb.service.IUse...

一角残叶的博客 609

Spring 整合(整合JDBC,Mybatis,Servlet)

Spring 整合持久层技术 Spring 整合 JDBC JDBCTemplate模板对象是Spring框架提供了的一个可以操作数据库的对象,封装了JDBC技术。在尽可能保留灵活性的情况下, 将数据库存取的工作量降到最低。 实例项目 创建项目和环境 创建项目,导入jar包 配置好tomcat 创建配置文件 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.spr

这个名字没什么特点的博客 272

Spring整合MyBatis和servlet

文章目录Spring整合MyBatis环境搭建配置文件Spring整合MyBatis和Servlet环境搭建 Spring整合MyBatis 环境搭建 配置文件 1.配置Spring的AppliactionContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframe...

ReverieZH的博客 562
上一篇: 使用JDBC实例理解数据库的事务隔离
下一篇: Spring MVC中Neither BindingResult nor plain target object for bean name 'user' available as request
fullStackChenZF
博客等级 码龄10年 44粉丝 70原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值