🎨 增加启动时显示项目访问地址的功能

This commit is contained in:
zhaojun1998
2019-12-24 22:11:45 +08:00
parent 844cbcc03a
commit e334acd508

View File

@@ -1,31 +1,58 @@
package im.zhaojun.common.util;
import cn.hutool.core.net.NetUtil;
import im.zhaojun.common.exception.InitializeException;
import im.zhaojun.common.service.FileAsyncCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.LinkedHashSet;
/**
* 项目启动监听器, 当项目启动时, 遍历当前对象存储的所有内容, 添加到缓存中.
* @author zhaojun
*/
@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
@Slf4j
public class StartupListener implements ApplicationListener<ApplicationStartedEvent> {
@Resource
private FileAsyncCacheService fileAsyncCacheService;
@Resource
private Environment environment;
@Override
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
public void onApplicationEvent(@NonNull ApplicationStartedEvent event) {
printStartInfo();
cacheAllFile();
}
private void printStartInfo() {
String serverPort = environment.getProperty("server.port", "8080");
LinkedHashSet<String> localIps = NetUtil.localIps();
StringBuilder indexAddr = new StringBuilder();
StringBuilder indexAdminAddr = new StringBuilder();
for (String localIp : localIps) {
String addr = String.format("http://%s:%s", localIp, serverPort);
indexAddr.append(addr).append("\t");
indexAdminAddr.append(addr).append("/#/admin").append("\t");
}
log.info("ZFile started at " + indexAddr);
log.info("ZFile Admin started at " + indexAdminAddr);
}
private void cacheAllFile() {
try {
fileAsyncCacheService.cacheGlobalFile();
} catch (Exception e) {
throw new InitializeException("初始化缓存异常.", e);
}
}
}