1: ServletRegistrationBean Servlet
@Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet"); return registrationBean; }
或者通过@WebServlet注解也可以.
2:FilterRegistrationBean 过滤器
@Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new MyFilter());
//过滤的请求路径 registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet")); return registrationBean; }
public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("MyFilter process..."); chain.doFilter(request,response); } @Override public void destroy() { } }
3:ServletListenerRegistrationBean 监听器
@Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener()); return registrationBean; }
myListener 实现 ServletContextListener即可