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;

}

相关推荐
JxWang057 分钟前
Task02:链表
后端
只会cv的前端攻城狮1 小时前
Elpis-Core — 融合 Koa 洋葱圈模型实现服务端引擎
前端·后端
codetown1 小时前
2026年Zig编程语言权威指南:从系统级底层架构到现代软件工程实践
后端·程序员
cg333 小时前
cc-connect,十分钟帮你把 claude code 连接到微信,飞书,钉钉等等平台
后端·openai
用户1427868669323 小时前
Java多态的底层真相:JVM到底怎么知道该调哪个方法?(面试高频)
后端
dkbnull3 小时前
深入理解Spring两大特性:IoC和AOP
spring boot
初次攀爬者3 小时前
RabbitMQ的消息模式和高级特性
后端·消息队列·rabbitmq
摸鱼的春哥3 小时前
惊!黑客靠AI把墨西哥政府打穿了,海量数据被黑
前端·javascript·后端
考虑考虑3 小时前
JDK25模块导入声明
java·后端·java ee
想用offer打牌5 小时前
高并发下如何保证接口的幂等性
后端·面试·状态机