Skip to content

Commit

Permalink
优化部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYuYang01 committed Jan 26, 2025
1 parent 363942e commit 6c9ffc5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 1 addition & 2 deletions blog/src/main/java/liuyuyang/net/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
@EnableFileStorage
@SpringBootApplication
public class Main {
public static void main(String[] args) {
System.out.println("服务已启动");
SpringApplication.run(Main.class, args);
}
}
19 changes: 13 additions & 6 deletions blog/src/main/java/liuyuyang/net/config/PrintConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liuyuyang.net.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent;
Expand All @@ -11,15 +12,16 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
@Component
public class PrintConfig {
private static final Logger logger = LoggerFactory.getLogger(PrintConfig.class);
private final ConfigurableEnvironment environment;
private final AtomicBoolean printed = new AtomicBoolean(false);

private static final Map<String, String> CONFIG_MAPPINGS = new LinkedHashMap<>();

static {
CONFIG_MAPPINGS.put("storage.domain", "DOMAIN");
CONFIG_MAPPINGS.put("server.port", "PORT");
CONFIG_MAPPINGS.put("spring.datasource.url", "DB_INFO");
CONFIG_MAPPINGS.put("spring.datasource.username", "DB_USERNAME");
Expand All @@ -28,9 +30,6 @@ public class PrintConfig {
CONFIG_MAPPINGS.put("lyy.email.port", "EMAIL_PORT");
CONFIG_MAPPINGS.put("lyy.email.username", "EMAIL_USERNAME");
CONFIG_MAPPINGS.put("lyy.email.password", "EMAIL_PASSWORD");
CONFIG_MAPPINGS.put("lyy.oss.accessKey", "OSS_ACCESS_KEY");
CONFIG_MAPPINGS.put("lyy.oss.secretKey", "OSS_SECRET_KEY");
CONFIG_MAPPINGS.put("lyy.oss.bucket", "OSS_BUCKET");
}

public PrintConfig(ConfigurableEnvironment environment) {
Expand All @@ -39,14 +38,22 @@ public PrintConfig(ConfigurableEnvironment environment) {

@EventListener
public void printConfigurations(ApplicationStartedEvent event) {
log.info("\n----------------------------------------------------------\n\t\t" +
"服务已启动: 欢迎使用 ThriveX 博客管理系统 \n\t\t" +
"接口地址: \thttp://localhost:{}/api\n\t\t" +
"API文档: \thttp://localhost:{}/doc.html\n\t\t" +
"加入项目交流群: liuyuyang2023\n" +
"----------------------------------------------------------",
environment.getProperty("server.port"), environment.getProperty("server.port"));

if (printed.compareAndSet(false, true)) {
logger.info("=== 配置信息 ===");

CONFIG_MAPPINGS.forEach((configPath, envVar) -> {
String value = environment.getProperty(configPath);
logger.info("{} = {}", envVar, value);
});

logger.info("=== 配置信息打印完成 ===");
}
}
Expand Down

0 comments on commit 6c9ffc5

Please sign in to comment.