0
0

SpringBootでシステム起動時に独自処理を追加(ログ出力など)

Last updated at Posted at 2024-04-14

この記事はこれの続きです

ApplicationRunnerクラスの実装

SpringBootにはApplicationRunnerという名前の抽象クラスが用意されています。
これを実装するとシステム起動時に独自処理を追加できます。

NekoApplicationRunner.java
package com.example.demo;


import java.net.InetAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


@Component
public class NekoApplicationRunner implements ApplicationRunner {
	
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	@Override
    public void run(ApplicationArguments args) throws Exception {
		
		
		String server_name_ = InetAddress.getLocalHost().getHostName();
		log.info("サーバー名" + server_name_);
		
		String ip_address_ = InetAddress.getLocalHost().getHostAddress();
		log.info("サーバーIPアドレス" + ip_address_);

        String tomcat_version_ = org.apache.catalina.util.ServerInfo.getServerInfo();
        log.info("Tomcatバージョン" + tomcat_version_);

        String java_version_ = System.getProperty("java.version");
        log.info("JAVAバージョン" + java_version_);
        
        String encoding = System.getProperty("file.encoding");
        log.info("ファイルエンコーディング" + encoding);
        
        String user_name_ = System.getProperty("user.name");
        log.info("システム実行ユーザー" + user_name_);
        
        
	}

 
}

実行結果

SpringBoot側の準備が終わった後に実行されます。
image.png

この記事の続き

バージョン

Microsoft Windows [Version 10.0.22631.3447]
JAVA 17.0.10
Spring Boot v3.1.10

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0