1、使用Servlet API(推荐)
先看看官网怎么说的
在主入口类上加@ServletComponetScan,并指定basePackages,即扫描的包
package demo.boot.demo03; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan(basePackages = "demo.boot.demo03") @SpringBootApplication public class Demo03Application { public static void main(String[] args) { SpringApplication.run(Demo03Application.class, args); } }
1.1 servlet(@WebServlet)
package demo.boot.demo03.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(urlPatterns = "/my") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("hahaha"); } }
运行起来:
1.2 Filter(@WebFilter)
package demo.boot.demo03.servlet; import lombok.extern.slf4j.Slf4j; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @Slf4j @WebFilter(urlPatterns = "/css/*") public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("MyFilter初始化完成"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("MyFilter工作"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { log.info("MyFilter销毁"); } }
1.3 Listener(@WebListener)
package demo.boot.demo03.servlet; import lombok.extern.slf4j.Slf4j; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @Slf4j @WebListener public class MyServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { log.info("监听到项目初始化完成"); } @Override public void contextDestroyed(ServletContextEvent sce) { log.info("监听到项目销毁"); } }
运行起来:
2、使用RegistrationBean
看看官网:
创建MyRegisterConfig类
package demo.boot.demo03.servlet; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Arrays; @Configuration(proxyBeanMethods = true) //保证依赖的组件始终是单实例的 public class MyRegisterConfig { @Bean public ServletRegistrationBean myServlet(){ MyServlet myServlet = new MyServlet(); //因为底层是urlMapping,所以可以传多个路径 return new ServletRegistrationBean(myServlet,"/my","/my02"); } @Bean public FilterRegistrationBean myFilter(){ MyFilter myFilter = new MyFilter(); //拦截servlet的路径 // return new FilterRegistrationBean(myFilter,myServlet()); FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter); filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*")); return filterRegistrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ MyServletContextListener myServletContextListener = new MyServletContextListener(); return new ServletListenerRegistrationBean(myServletContextListener); } }
将前面的MyServlet、MyListener、MyFilter的@Webxxx注释掉,这样@ServletComponetScan就不起作用了,我们只用这三个类的其他部分
运行起来: