国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

前端后端四川seo整站優(yōu)化

前端后端,四川seo整站優(yōu)化,wordpress 漢化 自適應,動態(tài)二維碼制作在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存,或者啟動后讀取相應的字典配置等,這篇文章主要聊聊實現(xiàn)立即執(zhí)行的幾種方法。 一、CommandLineRunner和ApplicationRunner 這兩者的實現(xiàn)方法…

在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存,或者啟動后讀取相應的字典配置等,這篇文章主要聊聊實現(xiàn)立即執(zhí)行的幾種方法。

一、CommandLineRunnerApplicationRunner

這兩者的實現(xiàn)方法一樣,都是去繼承相應的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項目中最常用的,比較靈活,有多個CommandLineRunnerApplicationRunner實現(xiàn)類時可以通過@Order注解或?qū)崿F(xiàn)Ordered接口重寫getOrder方法實現(xiàn)按指定順序執(zhí)行

1. CommandLineRunner的實現(xiàn)

 

java

復制代碼

import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class CommandLineRunnerImpl implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunnerImpl方法執(zhí)行"); } }

2. ApplicationRunner的實現(xiàn)

 

java

復制代碼

import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner方法執(zhí)行"); }

3. 多個CommandLineRunnerApplicationRunner實現(xiàn)類時指定順序執(zhí)行

通過@Order注解指定,數(shù)字越小越先執(zhí)行

 

java

復制代碼

@Component @Order(1) //通過order注解指定 public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner方法執(zhí)行"); } }

通過實現(xiàn)Ordered接口并重寫getOrder方法實現(xiàn),數(shù)字越小越先執(zhí)行

 

java

復制代碼

import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; @Component public class ApplicationRunnerImpl implements ApplicationRunner, Ordered { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner方法執(zhí)行"); } @Override public int getOrder() { return 1; } }

  1. CommandLineRunnerApplicationRunner的不同

這兩者的不同其實就是run方法中所接收的參數(shù)類型不同,CommandLineRunner是對我們啟動jar包時所傳參數(shù)不進行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過這個類型可以更方便的判斷某些參數(shù)是否存在之類的。

二、JDK所提供的@PostConstruct

@PostConstructJDK所提供的注解,使用該注解的方法會在服務器加載Servlet的時候運行。也可以在一個類中寫多個方法并添加這個注解。

 

java

復制代碼

import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class PostConstructTest { @PostConstruct public void start() { System.out.println("@PostConstruct方法執(zhí)行"); } @PostConstruct public void start01() { System.out.println("@PostConstruct1111方法執(zhí)行"); } }

三、其他方法(不常用)

1. ApplicationContextAware

 

java

復制代碼

import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextAwareImpl implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("ApplicationContextAwareImpl方法執(zhí)行"); } }

2. ApplicationListener(會執(zhí)行多次)

 

java

復制代碼

import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class ApplicationListenerImpl implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { System.out.println("ApplicationListenerImpl方法執(zhí)行"); } }

3. InitializingBean

 

java

復制代碼

import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @Component public class InitializingBeanImpl implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("InitializingBeanImpl方法執(zhí)行"); } }

這些方法也都可以實現(xiàn)在啟動項目后立即執(zhí)行,但是不太常用。

以上就是在SpringBoot項目中實現(xiàn)啟動項目后立即執(zhí)行方法的幾種方式,如果都大家有幫助,記得點贊、收藏哦,謝謝!

作者:JK凱
鏈接:https://juejin.cn/post/7237020208647569469
來源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

http://m.aloenet.com.cn/news/33050.html

相關(guān)文章:

  • iis網(wǎng)站壓縮百度搜索次數(shù)統(tǒng)計
  • 東營建設信息網(wǎng)公示專業(yè)整站優(yōu)化
  • php模板網(wǎng)站營銷推廣計劃怎么寫
  • ps怎么網(wǎng)站首頁網(wǎng)店推廣運營
  • 一浪網(wǎng)站建設競價網(wǎng)站
  • 網(wǎng)站優(yōu)化吧推廣發(fā)布任務平臺app下載
  • 做旅游網(wǎng)站包括哪些欄目成都百度業(yè)務員電話
  • 大數(shù)據(jù)開發(fā)平臺seo診斷優(yōu)化方案
  • 芯片設計公司重慶 seo
  • 免備案空間哪家好寧波seo服務推廣
  • 如今做哪些網(wǎng)站致富手機營銷推廣方案
  • 湖北廣域建設管理有限公司網(wǎng)站北京seo優(yōu)化哪家公司好
  • 廣西十佳旅游景區(qū)網(wǎng)站人多怎么優(yōu)化
  • 網(wǎng)站制作公司珠?;ヂ?lián)網(wǎng)輿情監(jiān)控系統(tǒng)
  • 3合1網(wǎng)站建設價格廣告投放平臺都有哪些
  • 建站公司服務費包括哪些查詢網(wǎng)站備案信息
  • qq空間是用什么做的網(wǎng)站成都網(wǎng)絡推廣外包公司哪家好
  • 網(wǎng)站策劃的步驟百度信息流開戶多少錢
  • 帝國cms做笑話網(wǎng)站宣傳渠道和宣傳方式有哪些
  • 湘潭關(guān)鍵詞優(yōu)化報價成都網(wǎng)站建設方案優(yōu)化
  • 廣州專業(yè)網(wǎng)站建設seo是啥意思
  • 徐州建設安全監(jiān)督網(wǎng)站搜索引擎seo
  • 做軟件的公司網(wǎng)站有哪些百度站長工具
  • 江蘇省工程建設信息網(wǎng)連云港seo優(yōu)化公司
  • 做網(wǎng)站改變圖片位置百度一下你就知道官網(wǎng)網(wǎng)址
  • frontpage做的網(wǎng)站好不好關(guān)鍵詞在線聽免費
  • 空間租用網(wǎng)站模板情感營銷的十大案例
  • 個人交互式網(wǎng)站備案鄭州百度推廣公司電話
  • c語言 做網(wǎng)站瀏覽器網(wǎng)站大全
  • 深圳網(wǎng)站建設微信開發(fā)長沙做搜索引擎的公司