SpringBoot与Servlet3.0整合注解
首先创建一个SpringBoot的启动类,添加@ServletComponentScan注解
@SpringBootApplication
@ServletComponentScan
public class SpringBootStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarter.class, args);
}
}
@WebServlet注解
在springboot中使用自定义的servlet
@WebServlet(name = "myServlet", urlPatterns = "/my/*")
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//do something
}
}
@WebFilter注解
在springboot中使用自定义的filter
@WebFilter(filterName = "myFilter", urlPatterns = "/my/*")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//do something
}
}
@WebListener注解
在springboot中使用自定义的listener
@WebListener
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//do something when starting
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//do something when stopping
}
}
@ServletComponentScan注解中的basePackages属性可以指定@WebServlet、@WebFilter和@WebListener所在的包路径