Springboot配置嵌入式服务器

一.如何定制和修改Servlet容器的相关配置

修改和server有关的配置(ServerProperties);

server.port=8081

server.context‐path=/tx

server.tomcat.uri‐encoding=UTF‐8

在yml配置文件的写法:

复制代码
server:
  port: 8081
  servlet:
    context-path: /tx

二.注册Servlet三大组件【Servlet、Filter、Listener】

由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。

1.servlet

//注册三大组件

@Bean

public ServletRegistrationBean myServlet(){

ServletRegistrationBean registrationBean = new ServletRegistrationBean(new

MyServlet(),"/myServlet");

return registrationBean;

}

2.FilterRegistrationBean

@Bean

public FilterRegistrationBean myFilter(){

FilterRegistrationBean registrationBean = new FilterRegistrationBean();

registrationBean.setFilter(new MyFilter());

registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));

return registrationBean;

}

3.ServletListenerRegistrationBean

@Bean

public ServletListenerRegistrationBean myListener(){

ServletListenerRegistrationBean<MyListener> registrationBean = new

ServletListenerRegistrationBean<>(new MyListener());

return registrationBean;

}

SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DispatcherServlet;

DispatcherServletAutoConfiguration中:

@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)

@ConditionalOnBean(value = DispatcherServlet.class, name =

DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)

public ServletRegistrationBean dispatcherServletRegistration(

DispatcherServlet dispatcherServlet) {

ServletRegistrationBean registration = new ServletRegistrationBean(

dispatcherServlet, this.serverProperties.getServletMapping());

//默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp

//可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径

registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);

registration.setLoadOnStartup(

this.webMvcProperties.getServlet().getLoadOnStartup());

if (this.multipartConfig != null) {

registration.setMultipartConfig(this.multipartConfig);

}

return registration;

}

相关推荐
掉头发的王富贵1 分钟前
【2025年终总结】对象有了,工作没了
java·后端·年终总结
Xの哲學2 分钟前
Linux自旋锁深度解析: 从设计思想到实战应用
linux·服务器·网络·数据结构·算法
SongYuLong的博客14 分钟前
openwrt 启动脚本
linux·运维·服务器·物联网
小旺不正经14 分钟前
n8n简介
linux·运维·服务器
lpfasd12314 分钟前
Spring Boot + WebFlux 全面使用指南
java·spring boot·后端
Cosolar18 分钟前
Java 后端访问 https接口报 SSLHandshakeException 你遇到过吗
java·后端·面试
m0_7482523826 分钟前
Foundation 表格的基本用法
开发语言·后端·rust
Mr.朱鹏26 分钟前
Spring Boot 配置文件加载顺序与优先级详解
java·spring boot·后端·spring·maven·配置文件·yml
洛阳泰山31 分钟前
一个人,一个项目,一年的坚持:关于我的 2025年 技术突围之路
java·人工智能·spring boot
程序员Agions35 分钟前
N+1 查询:那个让你 API 慢成 PPT 的隐形杀手
数据库·后端