SpringBoot原生組件注入實(shí)現(xiàn)兩種方式介紹
原生組件注入SpringBoot,即注冊(cè) Servlet 、Filter、Listener 進(jìn)入 SpringBoot
一、使用 Servlet API
使用 Servlet API 可以實(shí)現(xiàn)原生組件注入,通過(guò)在自定義 Servlet 前加入 @WebServlet 注釋,并且在 SpringBoot 啟動(dòng)類前加入 @ServletComponentScan 注釋,可實(shí)現(xiàn)注冊(cè) Servlet
代碼示例:
1、實(shí)現(xiàn)自定義 MyServlet
自定義 Servlet 類:
@WebServlet(urlPatterns = "/my") // 加入 @WebServlet 注釋
public class MyServlet extends HttpServlet { // 注意要繼承 HttpServlet 類
@Override // 重寫 DoGet 方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("haha");
}
}
項(xiàng)目啟動(dòng)類:
@ServletComponentScan(basePackages = "com.wanqing.admin") //掃描那個(gè)包中有servlet
@SpringBootApplication
public class DemoAdminApplication {
public static void main(String[] args) {
SpringApplication.run(DemoAdminApplication.class, args);
}
}
2、實(shí)現(xiàn)自定義 MyFilter
@Slf4j
@WebFilter(urlPatterns = {"/css/*", "/images/*"}) // 攔截靜態(tài)資源
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
log.info("MyFilter初始化完成");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("MyFilter工作");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
Filter.super.destroy();
log.info("MyFilter銷毀");
}
}3、實(shí)現(xiàn)自定義 MyServletContextListener
@WebListener
@Slf4j
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("MyServletContextListener 監(jiān)聽到項(xiàng)目初始化完成");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("MyServletContextListener 監(jiān)聽到項(xiàng)目銷毀");
}
}二、使用 RegistrationBean 的方式注入原生組件
通過(guò)編寫 MyRegistConfig 配置類,返回 RegistrationBean 的方式實(shí)現(xiàn)組件的注入,與上一種方式的區(qū)別在于,這種方式不需要給 自定義 Servlet 類寫 @WebServlet 注釋。
注意點(diǎn):要記得使用 @Bean 注釋將 ServletRegistrationBean 注冊(cè)到容器中。
代碼示例:
自定義 MyRegistConfig 配置類,注冊(cè) myServlet 組件,返回 ServletRegistrationBean 對(duì)象 (對(duì)象參數(shù)為自定義的 myServlet 對(duì)象實(shí)例)
myFilter 及myListener 的實(shí)現(xiàn)方式同理
@Configuration
public class MyRegistConfig {
@Bean
public ServletRegistrationBean myServlet(){
MyServlet myServlet = new MyServlet();
return new ServletRegistrationBean(myServlet, "/my","/my02");
}
@Bean
public FilterRegistrationBean myFilter(){
MyFilter filter = new MyFilter();
//return new FilterRegistrationBean(filter, myServlet()); // 攔截myServlet()的路徑
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter);
filterRegistrationBean.addUrlPatterns("/my","/css/*");
return filterRegistrationBean;
}
@Bean
public ServletListenerRegistrationBean myListener(){
MyServletContextListener myServletContextListener = new MyServletContextListener();
return new ServletListenerRegistrationBean(myServletContextListener);
}
}拓展:為什么攔截器不攔截 我們自定義的 MyServlet 請(qǐng)求?
分析 DispatcherServlet 如何注冊(cè)進(jìn)入容器中,從 DispatcherServletAutoConfiguration 類開始
容器中自動(dòng)配置了 DispatcherServlet 組件,其屬性綁定到 WebMvcProperties 中,對(duì)應(yīng)的配置文件是 spring.mvc
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) // 注冊(cè) DispatcherServlet 組件
public DispatcherServlet dispatcherServlet(WebMvcProperties webMvcProperties) {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
dispatcherServlet.setEnableLoggingRequestDetails(webMvcProperties.isLogRequestDetails());
return dispatcherServlet;
}
通過(guò) ServletRegistrationBean < DispatcherServlet > 機(jī)制(DispatcherServletRegistrationBean.class)將 DispatcherServlet 原生的 Servlet 組件配置進(jìn)來(lái)
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
webMvcProperties.getServlet().getPath()); // 拿到默認(rèn)映射路徑為 / 路徑
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}拿到默認(rèn)映射路徑 /
WebMvcProperties.class 中配置

使用 Tomcat 做原生 Servlet 開發(fā),如果多個(gè) Servlet 都能處理到同一層路徑,是精確優(yōu)先原則,例如:
A:/my/
B: /my/1
發(fā)送 /my/1 請(qǐng)求 B處理,而發(fā)送 /my/2 請(qǐng)求 A 處理
結(jié)論 : 來(lái)到 /my 不經(jīng)過(guò) / —— 精確匹配 /my 直接經(jīng) Tomcat 寫出響應(yīng),不經(jīng)過(guò) SpringMVC 的一系列流程,因此不被攔截器攔截,如下圖所示:

到此這篇關(guān)于SpringBoot原生組件注入實(shí)現(xiàn)兩種方式介紹的文章就介紹到這了,更多相關(guān)SpringBoot原生組件注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中instanceof關(guān)鍵字的用法總結(jié)
instanceof是Java的一個(gè)二元操作符,和==,>,<是同一類東東。由于它是由字母組成的,所以也是Java的保留關(guān)鍵字。它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類的實(shí)例,返回boolean類型的數(shù)據(jù)2013-10-10
MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù)
這篇文章主要介紹了MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
詳解MyBatis resultType與resultMap中的幾種返回類型
本文主要介紹了MyBatis resultType與resultMap中的幾種返回類型,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
spring*.xml配置文件明文加密的實(shí)現(xiàn)
這篇文章主要介紹了spring*.xml配置文件明文加密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例
在項(xiàng)目開發(fā)中,我們返回的數(shù)據(jù)或者對(duì)象沒(méi)有的時(shí)候一般直接返回的null,那么如何返回統(tǒng)一默認(rèn)值,感興趣的可以了解一下2021-07-07
關(guān)于SpringBoot的spring.factories文件詳細(xì)說(shuō)明
spring.factories 文件是 Spring Boot 自動(dòng)配置機(jī)制的核心部分之一,它位于每個(gè) Spring Boot 自動(dòng)配置模塊的 META-INF 目錄下,經(jīng)??吹?nbsp;spring.factories 文件,卻沒(méi)有對(duì)它進(jìn)行深入的了解和分析,今天我們就一起揭開面紗看看它的內(nèi)在,需要的朋友可以參考下2024-12-12
Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
解決mybatis分頁(yè)插件PageHelper導(dǎo)致自定義攔截器失效
這篇文章主要為大家介紹了解決mybatis分頁(yè)插件PageHelper導(dǎo)致自定義攔截器失效方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

