spring boot 启动前打印 环境变量
时间: 2023-08-23 13:06:39 浏览: 41
可以通过在 Spring Boot 的启动类中添加一个 `CommandLineRunner` 来实现在启动前打印环境变量。具体实现步骤如下:
1. 在启动类上添加 `@Component` 注解,将其注册为一个 Spring Bean。
```java
@SpringBootApplication
@Component
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// 在这里打印环境变量
Map<String, String> env = System.getenv();
env.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
```
2. 在 `run` 方法中,通过 `System.getenv()` 方法获取所有的环境变量,并使用 `forEach` 方法遍历打印出来。
```java
Map<String, String> env = System.getenv();
env.forEach((key, value) -> System.out.println(key + ": " + value));
```
启动应用后,就会在控制台上看到环境变量的信息。