SpringBoot-Web原生组件的注入

1、使用Servlet API

@ServletComponentScan(basePackages = “com.atjl.admin”) :指定原生Servlet组件都放在那里
@WebServlet(urlPatterns = “/my”) 效果:直接响应,没有经过Spring的拦截器
@WebFilter(urlPatterns={"/css/", "/images/*"})
@WebListener

扩展:

DispatchServlet 如何注册

  • 容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties,对应的配置文件配置项是 spring.mvc
  • 通过 ServletRegistrationBean 把 DispatcherServlet 配置进来
  • 默认映射的是 / 路径

2、使用RegistrationBean

ServletRegistrationBean, FilterRegistrationBean, 和 ServletListenerRegistrationBean

@Configuration
public class MyRegistConfig {

    @Bean
    public ServletRegistrationBean myServlet(){
        MyServlet myServlet = new MyServlet();

        return new ServletRegistrationBean(myServlet,"/my","/my02");
    }


    @Bean
    public FilterRegistrationBean myFilter(){

        MyFilter myFilter = new MyFilter();
//        return new FilterRegistrationBean(myFilter,myServlet());
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener(){
        MySwervletContextListener mySwervletContextListener = new MySwervletContextListener();
        return new ServletListenerRegistrationBean(mySwervletContextListener);
    }
}

你可能感兴趣的:(spring,boot,intellij,idea,java)