package com.shlb.timescaledbutils.config; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.env.Environment; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.sql.Connection; @Component @Slf4j public class StartupStatusPrinter implements ApplicationRunner { @Autowired private Environment environment; @Autowired private DataSource dataSource; @Autowired private RedisConnectionFactory redisConnectionFactory; @Override public void run(ApplicationArguments args) throws Exception { String port = environment.getProperty("server.port"); boolean postgresConnected = false; try (Connection connection = dataSource.getConnection()) { postgresConnected = connection.isValid(1000); } catch (Exception e) { log.error("PostgreSQL 连接失败: {}", e.getMessage()); } boolean redisConnected = false; try { String ping = redisConnectionFactory.getConnection().ping(); redisConnected = "PONG".equalsIgnoreCase(ping); } catch (Exception e) { log.error("Redis 连接失败: {}", e.getMessage()); } System.out.println("\n----------------------------------------------------------"); System.out.println("\tApplication is running! Access URLs:"); System.out.println("\tLocal: \t\thttp://localhost:" + port); System.out.println("\tPostgreSQL:\t" + (postgresConnected ? "Connected ✅" : "Failed ❌")); System.out.println("\tRedis:\t\t" + (redisConnected ? "Connected ✅" : "Failed ❌")); System.out.println("----------------------------------------------------------\n"); } }