2017-06-01 1 views

答えて

1

ServletContextListener

あなたは正確に「展開」とはどういう意味ですか?

サーブレットコンテナが実行時にサーブレットによって最初のリクエストが処理される前に、Webアプリケーションのコンテキストを作成している場合は、標準のフックを使用してコードを呼び出します瞬間

ServletContextListenerを実装し、必要なメソッドがcontextInitializedのクラスを作成します。デプロイ中にこのクラスに信号を送るには、@WebListenerと注釈を付けてください。多くの既存の質問のスタックオーバーフローを検索してください。&このトピックについての回答です。

この方法では、現在の瞬間をInstantとしてキャプチャします。タイムライン上では、ナノ秒の分解能でUTCで表されます。

Instant instant = Instant.now() ; 

コード例。

@WebListener 
public class MyServletContextListener implements ServletContextListener { 

    public void contextInitialized(ServletContextEvent sce) { 
     Instant instant = Instant.now() ; // Capture current moment in UTC. 
     // By default `toString` generates string in standard ISO 8601 format. 
     // Easy to parse: Instant.parse("2017-01-23T01:23:45.123456789Z"); 
     String instantIso8601 = instant.toString() ; 

     // Remember the launch time as an attribute on the context. 
     sce.getServletContext().setAttribute("launch_instant" , instantIso8601) ; 
     // Or save your moment in some class variable mentioned in Question. 
     someObjectOfSomeClass.setLaunchInstant(instant); 
    } 

    public void contextDestroyed(ServletContextEvent sce) { 
     … 
    } 

} 
0

アプリケーションの初期化フェーズでは、その変数をWARファイルのlast modification timeで初期化できます。

Mavenなどのツールを使用して、プロジェクトのプロパティとしてデプロイメント日付を設定する方が、WARファイルのパスが変更される可能性があるため、前述の方法よりも優れた方法です。

関連する問題