スプリングブートコンテナが起動してロードされた後、何かを行うためにいくつかのスレッドまたはプログラムを開く必要がある場合があります。この時点で、私たちがやりたいことを達成するために、contextrefreshedeventイベントを構成する必要があります
1。アプリケーションStartupクラス
Public Class ApplicationStartUpを実装ApplicationListener <contextrefreshedevent> {public void onapplicationEvent(contextrefreshedevent event){//コンテナをロードした後、DAOレイヤーを取得してデータベースossvideorepository ossvideore pository =(ossvideoriepository =(ossvideorepository) event.getApplicationContext()。getBean(ossvideorepository.class); //コンテナがロードされたら、構成ファイルserverconfig =(serverConfig)event.getApplicationContext()。getBean(serverConfig.class); serverFilesCanner filescanner = new ServerFilesCanner(ossvideorepository、serverConfig.getScanpath()); //コンテナがロードされた後にスレッドを起動するスレッドスレッド= newスレッド(filescanner); thread.start(); }}2。ServerConfigクラス
@component@configurationProperties(prefix = "server")public class serverconfig {private string aliyunossendpoint;プライベートストリングaliyunossaccesskeyid;プライベートストリングaliyunossaccesskeysecret;プライベートストリングaliyunossbucketname;プライベートストリングスキャンパス。 public string getaliyunossendpoint(){return aliyunossendpoint; } public void setaliyunossendpoint(string aliyunossendpoint){this.aliyunossendpoint = aliyunossendpoint; } public string getaliyunossaccesseyid(){return aliyunossacceskeyid; } public void setaliunossacceskeyid(string aliyunossacceskeyid){this.aliyunossacceskeyid = aliyunossacceskeyid; } public string getaliyunossaccesskeysecret(){return aliyunossaccesskeysecret; } public void setaliyunossacskeSecret(string aliyunossaccessecretret){this.aliyunossaccesskeysecret = aliyunossacceskeysecret; } public string getaliyunossbucketname(){return aliyunossbucketname; } public void setaliunossbucketname(string aliyunossbucketname){this.aliyunossbucketname = aliyunossbucketname; } public string getscanpath(){return scanpath; } public void setaliunossbucketname(string aliyunossbucketname){this.aliyunossbucketname = aliyunossbucketname; } public string getscanpath(){return scanpath; } public void setScanpath(String scanpath){this.scanpath = scanpath; }}PS:春に組み込まれたイベントもいくつかあります
1。ContexTrefreshedEvent:このイベントは、ApplicationContextコンテナが初期化または更新されたときにトリガーされます。
2。ContextStartEdevent:このイベントは、ApplicationContextコンテナがConfigureAbleAppLicationContextインターフェイスのstart()メソッドを使用して開始されたときにトリガーされます。
3。ContextClosedEvent:このイベントは、configureableapplicationContextインターフェイスのclose()メソッドを使用してapplicationContextコンテナが閉じられたときにトリガーされます。
4。ContextStopedEvent:このイベントは、configureableapplicationContextインターフェイスのstop()メソッドを使用してアプリケーションコンテキストコンテナを停止するとトリガーされます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。