[SpringBoot2]原生组件注入_原生注解与Spring方式注入

1、使用Servlet API

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

@ServletComponentScan(basePackages = "com.atguigu.admin")
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

public class Boot05WebAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(Boot05WebAdminApplication.class, args);
    }
    
}
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("66666");
    }
}
@Slf4j
@WebFilter(urlPatterns = {"/css/*","/images/*"})
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("init finished");
    }

    @Override
    public void destroy() {
        log.info("destroy finished");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("myFilter work");
        filterChain.doFilter(servletRequest,servletResponse);


    }
}

@Slf4j
@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("MyServletContextListener init finished");
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MyServletContextListener destory finished");
    }


}

2、使用RegistrationBean

@Slf4j
//@WebFilter(urlPatterns = {"/css/*","/images/*"})
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("init finished");
    }

    @Override
    public void destroy() {
        log.info("destroy finished");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("myFilter work");
        filterChain.doFilter(servletRequest,servletResponse);


    }
}



//=======================================================



//@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("66666");
    }



}

//=================================================================


@Slf4j
//@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("MyServletContextListener init finished");
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MyServletContextListener destory finished");
    }


}


@Configuration(proxyBeanMethods = true)
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()
    {
        MyServletContextListener myServletContextListener = new MyServletContextListener();
        return new ServletListenerRegistrationBean(myServletContextListener);
    }

}

注意:

@Configuration(proxyBeanMethods = true)

保证依赖的组件始终是单实例的

如果我们写成

@Configuration(proxyBeanMethods = false)

说明我们当前类,每次调用一次myServlet方法,就会创建一个新的MyServlet,有可能导致我们一调用下面的过滤器方法就会创建一个新的MyServlet,会导致我们容器中有很多冗余的其他对象!

return new FilterRegistrationBean(myFilter,myServlet());

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