SpringBoot Web開發(fā)之系統(tǒng)任務(wù)啟動與路徑映射和框架整合
本章概要
- 啟動系統(tǒng)任務(wù)
- 整合 Servlet、Filter 和 Listener
- 路徑映射
啟動系統(tǒng)任務(wù)
有一些特殊的任務(wù)需要在系統(tǒng)啟動時執(zhí)行,例如配置文件加載,數(shù)據(jù)庫初始化等操作。如果沒有使用 Spring Boot ,這些問題可以在 Listener 中解決。Spring Boot 對此提供了兩種解決方案 CommandLineRunner 和 ApplicationRunner。兩者基本一致,差別主要體現(xiàn)在參數(shù)上。
CommandLineRunner
Spring Boot 項目在啟動時會遍歷所有 CommandLineRunner 的實現(xiàn)類并調(diào)用其中的 run 方法,如果整個系統(tǒng)中有多個 CommandLineRunner 的實現(xiàn)類,那么可以使用 @Oder 注解對這些實現(xiàn)類的調(diào)用順序進(jìn)行排序。
在一個 Spring Boot Web 項目中(Spring Boot 項目引入 Web 依賴)添加兩個 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) 注解用來描述 CommandLineRunner 的執(zhí)行順序,數(shù)字越小越先執(zhí)行。
- run 方法中是調(diào)用的核心邏輯,參數(shù)是系統(tǒng)啟動時傳入的參數(shù),即入口類中 main 方法的參數(shù)(在調(diào)用 SpringApplication.run 方法時被傳入 Spring Boot 項目中)
在系統(tǒng)啟動時,配置傳入的參數(shù),以 IDEA 為例,配置方式如下:
步驟01:單擊右上角的編輯啟動配置,如圖:

步驟02:在打開的新頁面中編輯 Program arguments:

啟動項目,啟動日志如圖:

ApplicationRunner
在一個Spring Boot Web 項目中信件兩個 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 注解依然是用來描述執(zhí)行順序的,數(shù)字越小越優(yōu)先執(zhí)行
- @不同于 CommandLineRunner 中的 run 方法的 String 數(shù)組參數(shù),這里 run 方法的參數(shù)是一個 ApplicationArguments 對象,如果想從 ApplicationArguments 對象中獲取入口類中的 main 方法接收的參數(shù),調(diào)用 ApplicationArguments 中的 getNonOptionArgs 方法即可。ApplicationArguments 中的getOptionNames方法用來獲取項目啟動命令中參數(shù)的 key ,例如將本項目打成 jar 包,運行 java -jar xxx.jar -name=Michael 命令來啟動項目,此時 getOptionNames方法獲取到的就是 name ,而 getOptionValues 方法則是獲取相應(yīng)的 value。
打包程序,執(zhí)行如下命令啟動項目:
java -jar chapter04-0.0.1-SNAPSHOT.jar --name=Michael --age=99 斗羅大陸 唐家三少
日志如下:

整合Servlet與Filter和Listener
一般情況下,使用Spring 、 Spring MVC 這些框架后,基本上就告別 Servlet、Filter 、 Listerer 了,但是有時在整合一些第三方框架時,可能還是不得不使用 Servlet ,比如在整合某報表插件時就需要使用 Servlet。Spring Boot 中對于整合這些基本的 Web 組件也提供了很好的支持。
在一個 Spring Boot Web 項目中添加如下三個組件:
@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");
}
}
代碼解釋:
- 定義了三個基本的組件,分別使用 @WebServlet 、@WebFilter、@WebListener 三個注解進(jìn)行標(biāo)記
- 這里以 ServletRequestListener 為例,但是對于其他的 Listener ,例如 HttpSessionListener、ServletContextListener 等也是支持的
在項目入口類上添加 @ServletComponentScan 注解,實現(xiàn)對 Servlet、Filter 、Listener 的掃碼,如下:
@SpringBootApplication
@ServletComponentScan
public class Chapter04Application {
public static void main(String[] args) {
SpringApplication.run(Chapter04Application.class, args);
}
}
最后啟動項目,瀏覽器輸入"http://localhost:8081/my?name=Michael",可以看到相關(guān)日志,如下:MyListener>>>requestInitialized
MyFilter>>>doFilter
name>>>Michael
MyListener>>>requestDestroyed
路徑映射
一般情況下,使用了頁面模板后,用戶需要通過控制器才能訪問頁面。有一些頁面需要在控制器中加載數(shù)據(jù),然后渲染才能顯示出來;還有一些頁面在控制器中不需要加載數(shù)據(jù),只是完成簡單的跳轉(zhuǎn),對于這種頁面,可以直接配置路徑映射。例如,有兩個 Thymeleaf 做模板的頁面 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開發(fā)之系統(tǒng)任務(wù)啟動與路徑映射和框架整合的文章就介紹到這了,更多相關(guān)SpringBoot 框架整合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud Zuul自定義過濾器的實現(xiàn)
這篇文章主要介紹了自定義Spring Cloud Zuul過濾器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
使用Spring boot + jQuery上傳文件(kotlin)功能實例詳解
本文通過實例代碼給大家介紹了使用Spring boot + jQuery上傳文件(kotlin) 功能,需要的朋友可以參考下2017-07-07
SpringMVC后端返回數(shù)據(jù)到前端代碼示例
這篇文章主要介紹了SpringMVC后端返回數(shù)據(jù)到前端代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實現(xiàn)代碼
這篇文章主要介紹了基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實現(xiàn),會使用到多個數(shù)據(jù)源,文中通過代碼講解的非常詳細(xì),需要的朋友可以參考下2021-04-04
使用springboot的jar包能夠以service方式啟動
這篇文章主要介紹了使用springboot的jar包能夠以service方式啟動,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

