由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件。
注册三大组件用以下方式
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello,springBoot!");
}
}
@Configuration
public class ServletConfig {
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(), "/servlet");
registrationBean.setLoadOnStartup(1);//设置启动级别
return registrationBean;
}
}
SpringBoot帮我们自动SpringMVC的时候,自动的注册SpringMVC的前端控制器;DIspatcherServlet;
DispatcherServletAutoConfiguration:
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, webMvcProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}
DispatcherServletRegistrationBean是继承于ServletRegistrationBean, 通过DispatcherServletAutoConfiguration自动配置类注册了DispatcherServlet。
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter在过滤请求");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
.............................................................
@Bean
public FilterRegistrationBean myFilter(){
//设置过滤的Servlet请求
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter(), myServlet());
//这个也可以设置过滤的请求
filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello"));
return filterRegistrationBean;
}
..............................................................
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("销毁");
}
}
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new MyListener());
return bean;
}
编写servlet
@WebServlet(urlPatterns = {"/servlet"})
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello,servlet!!!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
主配置类上添加@ServletComponentScan注解,并指定编写的servlet的所在包,使其能够扫描到
@SpringBootApplication
@ServletComponentScan(basePackages = "com.wust.springboot.servlet")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}