SpringBoot(六):SpringBoot中如何使用Servlet?
第一种方法:
1.使用Servlet3的注解方式编写一个Servlet

2.在main方法的主类上添加注解:
@ServletComponentScan(basePackages = "com.example.springbootweb.servlet")
如图:

访问结果:

第二种方式:
通过springboot的配置类实现
1.编写一个普通的Servlet 类上没有注解

2.编写一个Springboot的配置类:
上面一张配置拦截器的时候写过配置类,这里我直接增加一个方法

代码如下:
package com.example.springbootweb.config; import com.example.springbootweb.interceptor.LoginInterceptor;
import com.example.springbootweb.servlet.HeServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration // 等价于一个spring的xml文件 比如applicationContext.xml
public class WebConfig implements WebMvcConfigurer {
/**
* @Bean 注解相当于spring 的xml配置中的一个
*
* <bean id="xxx" class="xxx.xxx.xxx.."></bean>
*方法名等于id
* 方法返回类型等于class
*
*
*/
@Bean
public ServletRegistrationBean heServletRegistrationBean(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HeServlet(),"/heServlet");
return registrationBean;
}
}
启动项目,访问结果:

SpringBoot(六):SpringBoot中如何使用Servlet?的更多相关文章
- SpringBoot(六) -- SpringBoot错误处理机制
一.SpringBoot中的默认的错误处理机制 1.在SpringBootWeb开发中,当我们访问请求出现错误时,会返回一个默认的错误页面: 2.在使用其他客户端访问的时候,则返回一个json数据: ...
- springboot(六)SpringBoot问题汇总
SpringBoot2.0整合Mybatis,取datetime数据类型字段出来时,发现少了8小时. 过程:mysql中注册时间查询出来结果是正确的,只是java程序运行出来后显示少了8小时.经前辈指 ...
- SpringBoot拦截器中Bean无法注入(转)
问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...
- springboot在eclipse中运行使用开发配置,打包后运行使用生产环境默认配置
java命令运行springboot jar文件,指定配置文件可使用如下两个参数中其中一个 --spring.config.location=配置文件路径 -Dspring.profiles.acti ...
- SpringBoot Mybatis项目中的多数据源支持
1.概述 有时项目里里需要抽取不同系统中的数据源,需要访问不同的数据库,本文介绍在Springboot+Mybatis项目中如何支持多数据源操作. 有需要的同学可以下载 示例代码 项目结构如下: 2. ...
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
- SpringBoot Web项目中中如何使用Junit
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
- [转] SpringBoot RESTful 应用中的异常处理小结
[From] https://segmentfault.com/a/1190000006749441 SpringBoot RESTful 应用中的异常处理小结 永顺 2016年08月29日发布 赞 ...
随机推荐
- 分块 && 例题 I Hate It HDU - 1754
分块算法: 分块就是对暴力方法的一种优化: _ 假设我们总共的序列长度为n,然后我们把它切成√n 块,然后把每一块里的东西当成一个整体来看,完整块:被 ...
- B-number HDU - 3652
题意: 找出区间[li,ri]有多少个符合要求的数: 1.这个数里面有13 2.这个数可以被13整除 题解: 这个题目和之前的有点不一样就是这个题目要我们求包含13的(之前做过的都是不包含).但是都差 ...
- EF Core数据访问入门
重要概念 Entity Framework (EF) Core 是轻量化.可扩展.开源和跨平台的数据访问技术,它还是一 种对象关系映射器 (ORM),它使 .NET 开发人员能够使用面向对象的思想处理 ...
- woj1012 Thingk and Count DP好题
title: woj1012 Thingk and Count DP好题 date: 2020-03-12 categories: acm tags: [acm,dp,woj] 难题,dp好题,几何题 ...
- element ui 渲染超过上百条数据时页面卡顿,更流畅的加载大量数据
问题:element ui table渲染上百条数据,页面渲染开始出现延时 解决方案:使用pl-table 注意:设置use-virtual并给定table高度
- 网络流学习-Ford-Fulkerson
首先我们先解决最大流问题 什么是最大流问题呢 根据我的理解,有一个源点s,汇点t,s可以通过一个网络(雾)流向汇点t 但是每一条边都有他的最大传输容量限制,那么我们的任务是,如何分配流量使得..从s流 ...
- tfrecords转图片存储
import os import shutil import tensorflow as tf import time import sys import cv2 # 图片存放位置 PATH_RES ...
- PostCSS All In One
PostCSS All In One https://postcss.org/ https://www.webpackjs.com/loaders/postcss-loader/ https://gi ...
- 如何在 Apple Watch S6上离线播放音乐
如何在 Apple Watch S6上离线播放音乐 Apple Watch 离线播放音乐 营销策略,捆绑销售 Apple Watch + AirPods + Apple Music Apple Wat ...
- Protocol Buffers All In One
Protocol Buffers All In One Protocol Buffers - Google's data interchange format Protocol buffers are ...