<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.5.RELEASEversion>
<relativePath/>
parent>
在SpringBoot的配置文件(application.properties,application.yaml)修改配置属性即可。
server相关配置:
tomcat相关配置:
Server和Tomcat配置相关联配置源码:
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
/**
* Server HTTP port.
*/
private Integer port;
/**
* Network address to which the server should bind.
*/
private InetAddress address;
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
/**
* Strategy for handling X-Forwarded-* headers.
*/
private ForwardHeadersStrategy forwardHeadersStrategy;
/**
* Value to use for the Server response header (if empty, no header is sent).
*/
private String serverHeader;
/**
* Maximum size of the HTTP message header.
*/
private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8);
/**
* Type of shutdown that the server will support.
*/
private Shutdown shutdown = Shutdown.IMMEDIATE;
@NestedConfigurationProperty
private Ssl ssl;
@NestedConfigurationProperty
private final Compression compression = new Compression();
@NestedConfigurationProperty
private final Http2 http2 = new Http2();
private final Servlet servlet = new Servlet();
private final Tomcat tomcat = new Tomcat();
private final Jetty jetty = new Jetty();
private final Netty netty = new Netty();
private final Undertow undertow = new Undertow();
}
该配置使用的SpringBoot版本为2.3.5.RELEASE,版本不同,配置可能有所差异。
@Configuration
public class WebConfig {
/*
* 基于Java代码配置Servlet容器
* */
@Bean
public ServletWebServerFactoryCustomizer serverConfig(){
ServerProperties serverProperties = new ServerProperties();
// 使用ServerProperties配置相关属性
serverProperties.setPort(8083);
serverProperties.getServlet().setContextPath("/crud");
// WebServer定制器
ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer = new ServletWebServerFactoryCustomizer(serverProperties);
return servletWebServerFactoryCustomizer;
}
}
Servlet:
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("serlvet请求响应。。。");
req.getRequestDispatcher("/result.html").forward(req,resp);
}
}
将该Servlet加入到配置类:
@Configuration
public class WebConfig {
/*
* SpringBoot配置Servlet
* */
@Bean
public ServletRegistrationBean<MyServlet> registrationServlet(){
ServletRegistrationBean<MyServlet> servletRegistrationBean = new ServletRegistrationBean<>(new MyServlet());
servletRegistrationBean.setUrlMappings(Arrays.asList("/servlet"));
return servletRegistrationBean;
}
}
Filter:
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter拦截请求。。。");
filterChain.doFilter(servletRequest,servletResponse);
}
}
将该Filter加入到配置类:
@Configuration
public class WebConfig {
/*
* SpringBoot配置Filter
* */
@Bean
public FilterRegistrationBean<MyFilter> registrationFilter(){
FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>(new MyFilter());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
Listener:
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Servlet容器注册。。。");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Servlet容器销毁。。。");
}
}
将该Listener加入配置类:
@Configuration
public class WebConfig {
/*
* SpringBoot配置Listener
* */
@Bean
public ServletListenerRegistrationBean<MyListener> registrationListener(){
ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return servletListenerRegistrationBean;
}
}
该配置类最终运行结果: