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日发布 赞 ...
随机推荐
- linux(4)Linux 文件内容查看
查看文件内容总览 cat 由第一行开始显示文件内容 tac 从最后一行开始显示,可以看出 tac 是 cat 的倒着写! nl 显示的时候,顺道输出行号! more 一页一页的显示文件内容 less ...
- 加快你ROS安装的一篇文章
前言: 首先ROS大家应该比较熟悉了哈,如果需要补充一下请看我之前的这篇文章 <嵌入式的我们为什么要学ROS>,对于嵌入式来说ROS是一个很好的进阶方向,所以如何快速的安装一个ROS到我们 ...
- 1. Machine Learning - Introduction
Speaker: Andrew Ng 1. Introduction 1.A comptuter program is said to learn from experience E with r ...
- Codeforces Round #622 (Div. 2) B. Different Rules(数学)
Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...
- HDU5286 wyh2000 and sequence【分块 均摊复杂度】
HDU5286 wyh2000 and sequence 题意: 给出长为\(N\)的序列\(A_1,A_2,A_3,\cdots,A_n\),\(q\)次询问,每次询问给出区间\([L,R]\),假 ...
- poj2001 Shortest Prefixes (trie树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- 【转】Dockerfile
1. 关于docker build docker build可以基于Dockerfile和context打包出一个镜像,其中context是一系列在PATH或URL中指定的位置中的文件(contex ...
- K8S(06)web管理方式-dashboard
K8S的web管理方式-dashboard 目录 K8S的web管理方式-dashboard 1 部署dashboard 1.1 获取dashboard镜像 1.1.1 获取1.8.3版本的dsash ...
- (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (127.0.0.1:3306)\n')
使用python 3.7 pymssql 连接本地mysql 5.6 报错 解决:参考 https://www.cnblogs.com/springbrotherhpu/p/11503139.html ...
- MDN All In One
MDN All In One https://github.com/mdn/ https://wiki.mozilla.org/MDN MDN 要凉了 https://developer.mozill ...