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;

}

相关推荐
ServBay2 分钟前
如何利用本地技术栈构建 0 成本 AI SaaS 雏形
后端·aigc·ai编程
菜鸟谢3 分钟前
Rust 集合 + 迭代器完整详解
后端
杨利杰YJlio6 分钟前
Codex桌面客户端上手:项目、插件与自动化实战
前端·后端
常铭11 分钟前
【Java基础】01-HashMap的底层原理
后端·面试
幼儿园技术家32 分钟前
实现 GEO 监控:从多引擎探测到优化闭环
前端·后端
掘金者阿豪1 小时前
微信小程序虚拟支付与广告转化回传实战记录
后端
ping某2 小时前
专栏-null 和 undefined 到底是什么?
前端·javascript·后端
神奇小汤圆2 小时前
别再只会用ArrayList了!Java集合框架的性能天花板到底在哪?
后端
神奇小汤圆2 小时前
Dubbo 的 SPI 和 JDK 的 SPI 有什么区别?
后端
叫我少年2 小时前
C# 字符串基础
后端