spring boot特性-基礎


キーワード
1.前回の回顧
前の節は基本的にspring bootプロジェクトの簡単な構築過程を理解しました.spring bootにも感性認識があります.過程の一環がどのような方式で私達の第一節の問題を解決したかも分かりました.
この節の主な内容はspring bootの基礎特性を簡単に知るためです.
2.目標
      SprigAplication類に関する基礎特性を理解する
  • Failure Analyzers
  • Banner
  • SpringApple
  • Fuent builder API
  • Appliation events and listeners
  • Web environment
  • Accessing appination argments
  • Using the Application Runner CommundLiner
  • Apple ext
  • Admin feat ures
  • 3.特性
  • Failure Analyzers
  • spring boot項目を実行すると様々な種類のexceptionが発生します.failure Anaylzerは対応する異常を捕獲して、異常を指定された読み取り可能な表示形式に変換することができます.
    カスタム:インターフェースを実現
    Failure Analyzerは、プロファイルMETA-INF/spring.factoresにカスタムの登録を行います. 
    Failure Analyzer類
    org.springframework.boot.diagnostics.FailureAnalyzer=\
    com.example.MyXXXXFailureAnalyzer
  • Banner
  • バナーはプロジェクトの広告情報であり、テキスト情報でもいいし、写真でもいいです.
    テキスト情報:
          classipathでbanner.txtテキストファイルを作成します.中には任意のテキストを配置することができます.また、プレースホルダを通してプロジェクトの基本情報を取得できます.
        ${application.version}/$
    具体的な意味は公式文書を参照してください.
    画像情報:
      テキストファイルを使う以外に、画像を指定することもできます.クラスパスでbanner.jpg/banner.png/banner.gifを作成します.
    上記のバナーの内容以外にも、いくつかの有用な構成があります.
    spring.main.banner-mode=off #banner  :off  、
    banner.charset=UTF-8 # Banner file encoding.
    banner.location=classpath:banner.txt # Banner file location.
    banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).
    banner.image.width= # Width of the banner image in chars (default 76)
    banner.image.height= # Height of the banner image in chars (default based on image height)
    banner.image.margin= # Left hand image margin in chars (default 2)
    banner.image.invert= # If images should be inverted for dark terminal themes (default false
  • springApple
  • springAppleicationはspring bootプロジェクトの核心であり、run静的な方法はプロジェクト起動入口である.ファイル構成ではなく、コードによっていくつかのspringAppleicationパラメータを構成したい場合、例えば、listenerならば、カスタムSpringApplicationのインスタンスを設定することができます.
    @SpringBootApplication
    public class App implements CommandLineRunner
    {
        public static void main( String[] args )
        {
            //      :
            //        ,       : :              Listener
            SpringApplication springApplication = new SpringApplication(App.class);
            springApplication.addListeners(new MyApplicationStarting());
            springApplication.addListeners(new MyApplicationEnvironmentPrepared());
            springApplication.addListeners(new MyApplicationPrepared());
            springApplication.addListeners(new MyApplicationReady());
            springApplication.addListeners(new MyApplicationFailed());
            springApplication.run(args);
    
        }
    }
  • Fuent builder API
  • フローアプリ方式でSprigApplitionを作成し、対応する構成を設定します.
    new SpringApplicationBuilder()
            .sources(Parent.class)
            .child(Application.class)
            .bannerMode(Banner.Mode.OFF)
            .run(args);
  • Appliation events and listeners
  • spring起動と運転中は異なるイベントを送信します.対応するlistenerを構成することで、springイベントを取得することができます.
    まず、springプロジェクトの起動によって送信されるイベントは、listenerを普通のbeanに配置すれば良いです.
    第二に、springプロジェクトの開始前または過程中のイベントについては、Application Contectより作成完了時間が早いので、事前に登録する必要があります.
             A:META-INF/spring.factoresプロファイルに配置されています.
    org.spring frame ework.co.ntxt.Appliation Listener=comp.demo.xxLisnter
            B:カスタム 
    SpringApplicationの例と対応するlistenerを配置します.
             
            SpringApplication springApplication = new SpringApplication(App.class);
            springApplication.addListeners(new MyApplicationStarting());
            springApplication.run(args); 
     
    典型的なイベントは以下の通りです.
    ApplicationStartingEvent
    ApplicationEnvironmentPreparedEvent
    ApplicationPreparedEvent
    ApplicationReadyEvent
    ApplicationFailedEvent
  • Web environment
  • web environmentつまりweb環境は、spring bootがwebプロジェクトを作成するかどうかを決定し、webプロジェクトであれば、spring MVCなどのweb構成をロードし、内蔵のtomcatを起動します.
    web environmentはデフォルトでは、クラスパスにクラスがあるかどうかを判断します.
    javax.servlet.Servletまたはorg.spring frame ebook.web.com ntxt.C.onfigrable WebApplication Context.
        springApplication.setWebEnvironment(false);       ,     servlet        
  • Accessing appination argments
  • メールメソッドのパラメータにアクセスします.コマンドラインからmainメソッドに送るパラメータをカスタマイズしたbeanで使用する場合があります.
    Aplication Agments Bean.
    以下のとおりです
    @Component
    public class MyBean {
    
        @Autowired
        ApplicationArguments args
    
    }
  • using the appications Listener
  •  プロジェクトの起動後に特殊な操作を実行したいなら、実現できます. Apple ListenerまたはCommundListener.このようなbeanはApplication Comptext refreshの後に呼び出します.
    このようなbeanが複数あれば、@を通してもいいです.
    Orderコメント指定実行順序
    import org.springframework.boot.*
    import org.springframework.stereotype.*
    
    @Component
    public class MyBean implements CommandLineRunner {
    
        public void run(String... args) {
            // Do something...
        }
    
    }
  • appication ext
  • spring bootプロジェクトが起動したら、jvmにオフの折り返しフックを登録して優雅なクローズを実現します.プロジェクトが終了すると、すべてが実現されました.
    DispposableBeanインターフェース、または配置しました.
    @Predestroyの注釈のbeanはいずれも呼び出されます.
    注意:直接にキルルを強制されると、これらのコールバックは呼び出されないので、これらのコールバックに重要な業務ロジックを置かないでください.
    4.次は
    今回は主にspring bootの基礎特性を理解しました.次のセクションではspring bootの重要な特性について話します.