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;

}

相关推荐
阿华的代码王国16 分钟前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Jimmy20 分钟前
AI 代理是什么,其有助于我们实现更智能编程
前端·后端·ai编程
小晶晶京京42 分钟前
day34-LNMP详解
linux·运维·服务器
画个太阳作晴天43 分钟前
A12预装app
linux·服务器·前端
AntBlack1 小时前
不当韭菜V1.1 :增强能力 ,辅助构建自己的交易规则
后端·python·pyqt
bobz9651 小时前
pip install 已经不再安全
后端
寻月隐君2 小时前
硬核实战:从零到一,用 Rust 和 Axum 构建高性能聊天服务后端
后端·rust·github
碎像2 小时前
Linux上配置环境变量
linux·运维·服务器
武昌库里写JAVA2 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
Pitayafruit3 小时前
Spring AI 进阶之路03:集成RAG构建高效知识库
spring boot·后端·llm