日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring boot注册三大组件

發(fā)布時(shí)間:2025/3/20 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring boot注册三大组件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

注冊(cè)三大組件

Spring boot
以jar包的方式
使用嵌入式的Tomcat啟動(dòng)Web應(yīng)用
沒有Web應(yīng)用目錄結(jié)構(gòu),沒有web.xml文件

Web應(yīng)用
是一個(gè)Web應(yīng)用的目錄結(jié)構(gòu)
存在目錄文件src/main/webapp/WEB-INF/web.xml
可以把三大組件,注冊(cè)在web.xml中

Spring boot
提供了三大組件,注冊(cè)方式

注冊(cè)Servlet,使用ServletRegistrationBean
注冊(cè)Filter,使用FilterRegistrationBean
注冊(cè)ServletListener,使用ServletListenerRegistrationBean

Servlet

自定義Servlet,繼承HttpServlet
重寫方法,處理請(qǐng)求

public class MyServlet extends HttpServlet {//處理get請(qǐng)求@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("Hello MyServlet");} }

Filter

自定義Filter,實(shí)現(xiàn)Filter接口
實(shí)現(xiàn)Filter的三個(gè)方法

Init,初始化
doFilter,過濾
destroy,銷毀
chain.doFilter,放行請(qǐng)求

public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.out.println("MyFilter process...");chain.doFilter(request,response);}@Overridepublic void destroy() {} }

Listener

自定義Listener,實(shí)現(xiàn)ServletContextListener
Listener有很多,這里監(jiān)聽啟動(dòng)和銷毀的Listener

contextInitialized,初始化方法,Web項(xiàng)目啟動(dòng)
contextDestroyed,銷毀方法,Web項(xiàng)目銷毀

public class MyListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("contextInitialized...web應(yīng)用啟動(dòng)");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("contextDestroyed...當(dāng)前web項(xiàng)目銷毀");} }

注冊(cè)Servlet

返回一個(gè)ServletRegistrationBean
是一個(gè)class,直接new一個(gè)
使用有參構(gòu)造器,傳遞自定義的Servlet,映射路徑

注冊(cè)Filter

返回一個(gè)FilterRegistrationBean
是一個(gè)class,直接new一個(gè)
設(shè)置自定義的Filter,設(shè)置攔截請(qǐng)求的url
是一個(gè)集合,使用Arrays轉(zhuǎn)換

注冊(cè)Listener

返回一個(gè)ServletListenerRegistrationBean
是一個(gè)類,直接new一個(gè)
傳遞自定義的Listener
可以注冊(cè)的Listener類型

注冊(cè)方式

@Configuration public class MyServerConfig {//注冊(cè)三大組件@Beanpublic ServletRegistrationBean myServlet(){ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");registrationBean.setLoadOnStartup(1);return registrationBean;}@Beanpublic FilterRegistrationBean myFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new MyFilter());registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));return registrationBean;}@Beanpublic ServletListenerRegistrationBean myListener(){ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());return registrationBean;}}

屬性設(shè)置

可以直接在registrationBean中設(shè)置
自定義組件的屬性

總結(jié)

以上是生活随笔為你收集整理的Spring boot注册三大组件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。