SpringBoot Web開(kāi)發(fā)之系統(tǒng)任務(wù)啟動(dòng)與路徑映射和框架整合
本章概要
- 啟動(dòng)系統(tǒng)任務(wù)
- 整合 Servlet、Filter 和 Listener
- 路徑映射
啟動(dòng)系統(tǒng)任務(wù)
有一些特殊的任務(wù)需要在系統(tǒng)啟動(dòng)時(shí)執(zhí)行,例如配置文件加載,數(shù)據(jù)庫(kù)初始化等操作。如果沒(méi)有使用 Spring Boot ,這些問(wèn)題可以在 Listener 中解決。Spring Boot 對(duì)此提供了兩種解決方案 CommandLineRunner 和 ApplicationRunner。兩者基本一致,差別主要體現(xiàn)在參數(shù)上。
CommandLineRunner
Spring Boot 項(xiàng)目在啟動(dòng)時(shí)會(huì)遍歷所有 CommandLineRunner 的實(shí)現(xiàn)類并調(diào)用其中的 run 方法,如果整個(gè)系統(tǒng)中有多個(gè) CommandLineRunner 的實(shí)現(xiàn)類,那么可以使用 @Oder 注解對(duì)這些實(shí)現(xiàn)類的調(diào)用順序進(jìn)行排序。
在一個(gè) Spring Boot Web 項(xiàng)目中(Spring Boot 項(xiàng)目引入 Web 依賴)添加兩個(gè) CommandLineRunner,分別如下:
@Component
@Order(1)
public class MyCommandLineRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner1>>>"+ Arrays.toString(args));
}
}
@Component
@Order(2)
public class MyCommandLineRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Runner2>>>"+Arrays.toString(args));
}
}
代碼解釋:
- @Order(1) 注解用來(lái)描述 CommandLineRunner 的執(zhí)行順序,數(shù)字越小越先執(zhí)行。
- run 方法中是調(diào)用的核心邏輯,參數(shù)是系統(tǒng)啟動(dòng)時(shí)傳入的參數(shù),即入口類中 main 方法的參數(shù)(在調(diào)用 SpringApplication.run 方法時(shí)被傳入 Spring Boot 項(xiàng)目中)
在系統(tǒng)啟動(dòng)時(shí),配置傳入的參數(shù),以 IDEA 為例,配置方式如下:
步驟01:?jiǎn)螕粲疑辖堑木庉媶?dòng)配置,如圖:

步驟02:在打開(kāi)的新頁(yè)面中編輯 Program arguments:

啟動(dòng)項(xiàng)目,啟動(dòng)日志如圖:

ApplicationRunner
在一個(gè)Spring Boot Web 項(xiàng)目中信件兩個(gè) ApplicationRunner,代碼如下:
@Component
@Order(2)
public class MyApplicationRunner1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
List<String> nonOptionArgs = args.getNonOptionArgs();
System.out.println("1-nonOptionArgs>>>" + nonOptionArgs);
Set<String> optionNames = args.getOptionNames();
for (String optionName : optionNames) {
System.out.println("1-key:" + optionName + ";value:" +
args.getOptionValues(optionName));
}
}
}
@Component
@Order(1)
public class MyApplicationRunner2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
List<String> nonOptionArgs = args.getNonOptionArgs();
System.out.println("2-nonOptionArgs>>>" + nonOptionArgs);
Set<String> optionNames = args.getOptionNames();
for (String optionName : optionNames) {
System.out.println("2-key:" + optionName + ";value:" +
args.getOptionValues(optionName));
}
}
}
代碼解釋:
- @Order 注解依然是用來(lái)描述執(zhí)行順序的,數(shù)字越小越優(yōu)先執(zhí)行
- @不同于 CommandLineRunner 中的 run 方法的 String 數(shù)組參數(shù),這里 run 方法的參數(shù)是一個(gè) ApplicationArguments 對(duì)象,如果想從 ApplicationArguments 對(duì)象中獲取入口類中的 main 方法接收的參數(shù),調(diào)用 ApplicationArguments 中的 getNonOptionArgs 方法即可。ApplicationArguments 中的getOptionNames方法用來(lái)獲取項(xiàng)目啟動(dòng)命令中參數(shù)的 key ,例如將本項(xiàng)目打成 jar 包,運(yùn)行 java -jar xxx.jar -name=Michael 命令來(lái)啟動(dòng)項(xiàng)目,此時(shí) getOptionNames方法獲取到的就是 name ,而 getOptionValues 方法則是獲取相應(yīng)的 value。
打包程序,執(zhí)行如下命令啟動(dòng)項(xiàng)目:
java -jar chapter04-0.0.1-SNAPSHOT.jar --name=Michael --age=99 斗羅大陸 唐家三少
日志如下:

整合Servlet與Filter和Listener
一般情況下,使用Spring 、 Spring MVC 這些框架后,基本上就告別 Servlet、Filter 、 Listerer 了,但是有時(shí)在整合一些第三方框架時(shí),可能還是不得不使用 Servlet ,比如在整合某報(bào)表插件時(shí)就需要使用 Servlet。Spring Boot 中對(duì)于整合這些基本的 Web 組件也提供了很好的支持。
在一個(gè) Spring Boot Web 項(xiàng)目中添加如下三個(gè)組件:
@WebServlet("/my")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
System.out.println("name>>>"+req.getParameter("name"));
}
}
@WebServlet("/my")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
System.out.println("name>>>"+req.getParameter("name"));
}
}
@WebFilter("/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig){
System.out.println("MyFilter>>>init");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("MyFilter>>>doFilter");
chain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("MyFilter>>>destroy");
}
}
@WebListener
public class MyListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("MyListener>>>requestDestroyed");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("MyListener>>>requestInitialized");
}
}
代碼解釋:
- 定義了三個(gè)基本的組件,分別使用 @WebServlet 、@WebFilter、@WebListener 三個(gè)注解進(jìn)行標(biāo)記
- 這里以 ServletRequestListener 為例,但是對(duì)于其他的 Listener ,例如 HttpSessionListener、ServletContextListener 等也是支持的
在項(xiàng)目入口類上添加 @ServletComponentScan 注解,實(shí)現(xiàn)對(duì) Servlet、Filter 、Listener 的掃碼,如下:
@SpringBootApplication
@ServletComponentScan
public class Chapter04Application {
public static void main(String[] args) {
SpringApplication.run(Chapter04Application.class, args);
}
}
最后啟動(dòng)項(xiàng)目,瀏覽器輸入"http://localhost:8081/my?name=Michael",可以看到相關(guān)日志,如下:MyListener>>>requestInitialized
MyFilter>>>doFilter
name>>>Michael
MyListener>>>requestDestroyed
路徑映射
一般情況下,使用了頁(yè)面模板后,用戶需要通過(guò)控制器才能訪問(wèn)頁(yè)面。有一些頁(yè)面需要在控制器中加載數(shù)據(jù),然后渲染才能顯示出來(lái);還有一些頁(yè)面在控制器中不需要加載數(shù)據(jù),只是完成簡(jiǎn)單的跳轉(zhuǎn),對(duì)于這種頁(yè)面,可以直接配置路徑映射。例如,有兩個(gè) Thymeleaf 做模板的頁(yè)面 login.html 和 index.html ,直接在 addViewControllers 方法配置映射關(guān)系即可:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/login").setViewName("login");
registry.addViewController("/index").setViewName("index");
}
}
到此這篇關(guān)于SpringBoot Web開(kāi)發(fā)之系統(tǒng)任務(wù)啟動(dòng)與路徑映射和框架整合的文章就介紹到這了,更多相關(guān)SpringBoot 框架整合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 項(xiàng)目中的圖片處理策略之本地存儲(chǔ)與路徑映射
- Springboot如何通過(guò)路徑映射獲取本機(jī)圖片資源
- SpringBoot重寫(xiě)addResourceHandlers映射文件路徑方式
- springboot文件虛擬路徑映射方式
- 使用springboot訪問(wèn)圖片本地路徑并映射成url
- 使用SpringBoot設(shè)置虛擬路徑映射絕對(duì)路徑
- SpringBoot路徑映射實(shí)現(xiàn)過(guò)程圖解
- SpringBoot圖片上傳和訪問(wèn)路徑映射
- SpringBoot路徑映射配置的實(shí)現(xiàn)步驟
相關(guān)文章
java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器,模擬血壓計(jì)實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Spring Cloud Zuul自定義過(guò)濾器的實(shí)現(xiàn)
這篇文章主要介紹了自定義Spring Cloud Zuul過(guò)濾器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
使用Spring boot + jQuery上傳文件(kotlin)功能實(shí)例詳解
本文通過(guò)實(shí)例代碼給大家介紹了使用Spring boot + jQuery上傳文件(kotlin) 功能,需要的朋友可以參考下2017-07-07
SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例
這篇文章主要介紹了SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn),會(huì)使用到多個(gè)數(shù)據(jù)源,文中通過(guò)代碼講解的非常詳細(xì),需要的朋友可以參考下2021-04-04
使用springboot的jar包能夠以service方式啟動(dòng)
這篇文章主要介紹了使用springboot的jar包能夠以service方式啟動(dòng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

