[SpringBoot2]原生组件注入_原生注解与Spring方式注入
Posted 唐火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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方式注入的主要内容,如果未能解决你的问题,请参考以下文章
四SpringBoot2核心技术——容器功能(组件添加&原生配置文件引入&配置绑定)
四SpringBoot2核心技术——容器功能(组件添加&原生配置文件引入&配置绑定)
SpringBoot------Servlet3.0的注解自定义原生Listener监听器