Spring Boot Servlet#

注册方式#

servlet3.0注解+@ServletComponentScan#

一系列以@Web*开头的注解:@WebServlet@WebFilter@WebListener

@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet{}
@WebFilter("/hello/*")
public class HelloWorldFilter implements Filter {}
@SpringBootApplication
@ServletComponentScan
public class SpringBootServletApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringBootServletApplication.class, args);
	}
}

RegistrationBean#

@Bean
public ServletRegistrationBean helloWorldServlet() {
	ServletRegistrationBean helloWorldServlet = new ServletRegistrationBean();
	myServlet.addUrlMappings("/hello");
	myServlet.setServlet(new HelloWorldServlet());
	return helloWorldServlet;
}

@Bean
public FilterRegistrationBean helloWorldFilter() {
	FilterRegistrationBean helloWorldFilter = new FilterRegistrationBean();
	myFilter.addUrlPatterns("/hello/*");
	myFilter.setFilter(new HelloWorldFilter());
	return helloWorldFilter;
}

RegistrationBean-min-min

TomcatStarter-min-min

EmbeddedWebApplicationContext加载流程总结#

  • EmbeddedWebApplicationContextonRefresh方法触发配置了一个匿名的ServletContextInitializer

  • 匿名的ServletContextInitializeronStartup方法会去容器中搜索到所有的RegisterBean,并按照顺序加载到ServletContext

  • 匿名的ServletContextInitializer最终传递给TomcatStarter,由TomcatStarteronStartup方法去触发ServletContextInitializeronStartup方法,最终完成装配

Custom#

@Configuration
public class CustomServletContextInitializer implements ServletContextInitializer {
	private final static String JAR_HELLO_URL = "/hello";
	
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		System.out.println("创建 helloWorldServlet...");
		ServletRegistration.Dynamic servlet = servletContext.addServlet(HelloWorldServlet.class.getSimpleName(), HelloWorldServlet.class);
		servlet.addMapping(JAR_HELLO_URL);
		System.out.println("创建 helloWorldFilter...");
		FilterRegistration.Dynamic filter = servletContext.addFilter(HelloWorldFilter.class.getSimpleName(), HelloWorldFilter.class);
		EnumSet<DispatcherType> dispatcherTypes = EnumSet.allOf(DispatcherType.class);
		dispatcherTypes.add(DispatcherType.REQUEST);
		dispatcherTypes.add(DispatcherType.FORWARD);
		filter.addMappingForUrlPatterns(dispatcherTypes, true, JAR_HELLO_URL);
	}
}

References#