配置嵌入式Servlet容器
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器;
1)、如何定制和修改Servlet容器的相关配置;
- 方法一:修改和server有关的配置(ServerProperties);
server.port=8081
server.context-path=/Slgod
server.tomcat.uri-encoding=UTF-8
//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx
- 方法二:编写一个EmbeddedServletContainerCustomizer(嵌入式的Servlet容器的定制器)
@Configuration
public class MyServerConfig {
//加入到容器中
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
return new EmbeddedServletContainerCustomizer(){
//定制嵌入式的Servlet容器相关的规则
@Override
public void customize(ConfigurableEmbeddedServletContainer container){
container.setPort(8083);
}
};
}
注册Servlet三大组件【Servlet,Filter,Listener】
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的Web应用,没有web.xml文件。
注册三大组件用以下方式
ServletRegistrationBean
@Configuration
public class MyServerConfig {
//注册三大组件
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServler(),"/myServlet");
return registrationBean;
}
FilterRegistrationBean
package com.Slgod.springboot.filter
public class Myfilter implements Filter {
//Filter的初始化
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
//Filter的过滤
@Override
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException {
System.out.println("MyFilter process...");
chain.doFilter(request,response);
}
//Filter的销毁
@Override
public void destroy(){
}
}
@Configuration
public class MyServerConfig {
//注册三大组件
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServler(),"/myServlet");
return registrationBean;
}
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrarys.asList("/hello","/myServlet","/Slgod"));
return registrationBean;
}
ListenerRegistrationBean
package com.Slgod.springboot.listener;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce){
System.out.println("很高兴我在写代码的时候,你在打游戏,just going on");
}
@Override
public void contextDestroyed(ServletContextEvent sce){
System.out.println("很高兴我在写代码的时候,你在刷抖音,just going on");
}
}
@Configuration
public class MyServerConfig {
//注册三大组件
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServler(),"/myServlet");
//设置启动顺序
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrarys.asList("/hello","/myServlet","/Slgod"));
return registrationBean;
}
@Bean
public ListenerRegistrationBean myListener(){
ListenerRegistrationBean<MyListener> registrationBean = new ListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
使用其他的Servlet容器
Tomcat(默认使用)
<!--引入web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--引入web模块默认使用嵌入式的Tomcat作为Servlet容器-->
</dependency>
Jetty(适用于长连接的应用)
<!--引入web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入其他的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
Undertow(不支持JSP,优点:高性能,非阻塞的Servlet,并发性能非常不错)
<!--引入web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入其他的Servlet容器-->
<dependency>
<artifactId>spring-boot-starter-undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>