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日发布 赞 ...
随机推荐
- 聊聊elasticsearch7.8的模板和动态映射
最近想写一篇es的索引的一个设计,由于设计的东西特别多,当然,elasticsearch的模板和动态映射也是其中的一个设计点,所以干脆先来聊聊索引的模板和动态映射,模板,听这个名字就相当于一些公共可用 ...
- PostgreSQL 实现定时任务的 4 种方法
数据库定时任务可以用于实现定期的备份.统计信息采集.数据汇总.数据清理与优化等.PostgreSQL 没有提供类似 Oracle.MySQL 以及 Microsoft SQL Sever 的内置任务调 ...
- BZOJ3998 弦论 【SAM】k小子串
BZOJ3998 弦论 给一个字符串,问其第\(K\)小字串是什么 两种形式 1.不同起始位置的相同串只算一次 2.不同起始位置的相同串各算一次 首先建\(SAM\) 所有串的数量就是\(SAM\)中 ...
- DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂
It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...
- HTTP笔记4--HTTP 状态码
状态码作用.组成 HTTP 状态码负责表示客户端 HTTP 请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作.状态码的职责是当客户端向服务器端发送请求时,描述返回的请求结果. 状态码如 ...
- Nginx基础 - 通用优化配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf user nginx; worker_processes auto; worker_cpu_affinity ...
- 生成不带签名(BOM)的UTF8格式的XML
生成XML的一种方法如下: using System.Xml; private void SaveXML(string savePath) { XmlWriterSettings setting = ...
- Leetcode(22)-括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- SVG background watermark
SVG background watermark SVG 背景水印 <svg xmlns="http://www.w3.org/2000/svg" width="2 ...
- JavaScript var, let, const difference All In One
JavaScript var, let, const difference All In One js var, let, const 区别 All In One 是否存在 hoisting var ...