1.修改启动类打印,让启动类简洁,让打印内容可扩展
2.使用配置文件配合注解操作模块化的加载,可以通过配置文件控制加载哪些模块,同时前端隐藏未开启模块
This commit is contained in:
@@ -43,19 +43,6 @@ public class Application extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
|
||||
Environment env = application.getEnvironment();
|
||||
String contextPath = env.getProperty("server.servlet.context-path");
|
||||
contextPath = Optional.ofNullable(contextPath).orElse("").replaceFirst("/", "");
|
||||
contextPath = (contextPath.length() <= 0 || contextPath.endsWith("/")) ? contextPath : contextPath + "/";
|
||||
String hostAddress = InetAddress.getLocalHost().getHostAddress();
|
||||
String serverPort = env.getProperty("server.port");
|
||||
String urlCtx = hostAddress + ":" + serverPort + "/" + contextPath;
|
||||
logger.info("\n----------------------------------------------------------\n\t" +
|
||||
"\tzyplayer-doc启动完成,当前版本:{}\n" +
|
||||
"\t访问地址:http://{}\n" +
|
||||
"----------------------------------------------------------",
|
||||
ZyplayerDocVersion.version, urlCtx
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,11 @@ package com.zyplayer.doc.manage.framework.config;
|
||||
import com.zyplayer.doc.api.framework.config.EnableDocApi;
|
||||
import com.zyplayer.doc.db.framework.configuration.EnableDocDb;
|
||||
import com.zyplayer.doc.wiki.framework.config.EnableDocWiki;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -15,69 +18,25 @@ import java.util.HashMap;
|
||||
* @author 暮光:城中城
|
||||
* @author Sh1yu 2023年6月15日
|
||||
* @since 2019年3月31日
|
||||
* 规范:添加模块的类,命名需要和前端接受模块开启状态的参数一致,即 enbaleXxxxx enable模块名
|
||||
*/
|
||||
@Configuration
|
||||
public class ZyplayerDocConfig {
|
||||
//wiki模块的开启配置
|
||||
@Value("${zyplayer.doc.manage.enable.wiki:true}")
|
||||
private boolean enableWiki;
|
||||
|
||||
//db模块的开启配置
|
||||
@Value("${zyplayer.doc.manage.enable.db:true}")
|
||||
private boolean enableDb;
|
||||
|
||||
//api模块的开启配置
|
||||
@Value("${zyplayer.doc.manage.enable.api:true}")
|
||||
private boolean enableApi;
|
||||
|
||||
public class ZyplayerDocConfig {
|
||||
@EnableDocWiki
|
||||
//wiki模块加载注解条件化,配合配置文件决定是否加载
|
||||
@ConditionalOnProperty(prefix = "zyplayer.doc.manage.enable", name = "wiki", matchIfMissing = true)
|
||||
public class enableDocWiki {
|
||||
public class enableWiki {
|
||||
}
|
||||
|
||||
@EnableDocDb
|
||||
//db模块加载注解条件化,配合配置文件决定是否加载
|
||||
@ConditionalOnProperty(prefix = "zyplayer.doc.manage.enable", name = "db", matchIfMissing = true)
|
||||
public class enableDocDb {
|
||||
public class enableDb {
|
||||
}
|
||||
|
||||
@EnableDocApi
|
||||
//api模块加载注解条件化,配合配置文件决定是否加载
|
||||
@ConditionalOnProperty(prefix = "zyplayer.doc.manage.enable", name = "api", matchIfMissing = true)
|
||||
public class enableDocApi {
|
||||
}
|
||||
|
||||
public boolean isEnableWiki() {
|
||||
return enableWiki;
|
||||
}
|
||||
|
||||
public void setEnableWiki(boolean enableWiki) {
|
||||
this.enableWiki = enableWiki;
|
||||
}
|
||||
|
||||
public boolean isEnableDb() {
|
||||
return enableDb;
|
||||
}
|
||||
|
||||
public void setEnableDb(boolean enableDb) {
|
||||
this.enableDb = enableDb;
|
||||
}
|
||||
|
||||
public boolean isEnableApi() {
|
||||
return enableApi;
|
||||
}
|
||||
|
||||
public void setEnableApi(boolean enableApi) {
|
||||
this.enableApi = enableApi;
|
||||
}
|
||||
|
||||
//提供模块开启状态数组,给前端控制页面展示
|
||||
public HashMap<String,Boolean> getMoudleInfo(){
|
||||
HashMap<String,Boolean> moudleInfo = new HashMap<>();
|
||||
moudleInfo.put("enableWiki",this.enableWiki);
|
||||
moudleInfo.put("enableDb",this.enableDb);
|
||||
moudleInfo.put("enableApi",this.enableApi);
|
||||
return moudleInfo;
|
||||
public class enableApi {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.zyplayer.doc.manage.framework.config;
|
||||
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* 按照类加载的情况获取模块的加载状态
|
||||
*
|
||||
* @author Sh1yu
|
||||
* @since 2023年6月15日
|
||||
*/
|
||||
@Configuration
|
||||
public class ZyplayerMoudleKeeper implements ApplicationContextAware {
|
||||
HashMap<String, Boolean> moudleInfo = new HashMap<>();
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
//获取模块是否启动
|
||||
public boolean isMoudleStarted(Class<?> clazz) {
|
||||
if (moudleInfo.size() < 1) {
|
||||
getMoudleInfo();
|
||||
}
|
||||
return moudleInfo.get(clazz.getName().split("\\$")[1]);
|
||||
}
|
||||
|
||||
//提供模块开启状态数组,给前端控制页面展示
|
||||
public HashMap<String, Boolean> getMoudleInfo() {
|
||||
if (moudleInfo.size() < 1) {
|
||||
synchronized (ZyplayerMoudleKeeper.class) {
|
||||
Class<? extends ZyplayerDocConfig> clazz = ZyplayerDocConfig.class;
|
||||
Class<?>[] innerClasses = clazz.getClasses();
|
||||
for (Class<?> innerClass : innerClasses) {
|
||||
moudleInfo.put(innerClass.getName().split("\\$")[1], isMoudleConfigLoadUp(innerClass));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return moudleInfo;
|
||||
}
|
||||
|
||||
private Boolean isMoudleConfigLoadUp(Class<?> innerClass) {
|
||||
Object bean = null;
|
||||
try {
|
||||
bean = applicationContext.getBean(innerClass);
|
||||
} catch (BeansException e) {
|
||||
return false;
|
||||
}
|
||||
return null != bean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.zyplayer.doc.manage.framework.console;
|
||||
|
||||
import com.zyplayer.doc.core.util.ZyplayerDocVersion;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 启动后打印应用信息
|
||||
*
|
||||
* @author Sh1yu
|
||||
* @since 2023年6月16日
|
||||
*/
|
||||
@Component
|
||||
public class ApplicationInfoConsolePrint implements IConsolePrint {
|
||||
@Resource
|
||||
Environment environment;
|
||||
|
||||
@Override
|
||||
public void buildPrintInfo(StringBuffer printInfo) throws Exception {
|
||||
String contextPath = environment.getProperty("server.servlet.context-path");
|
||||
contextPath = Optional.ofNullable(contextPath).orElse("").replaceFirst("/", "");
|
||||
contextPath = (contextPath.length() <= 0 || contextPath.endsWith("/")) ? contextPath : contextPath + "/";
|
||||
String hostAddress = InetAddress.getLocalHost().getHostAddress();
|
||||
String serverPort = environment.getProperty("server.port");
|
||||
String urlCtx = hostAddress + ":" + serverPort + "/" + contextPath;
|
||||
printInfo.append("\t zyplayer-doc启动完成,当前版本:")
|
||||
.append(ZyplayerDocVersion.version)
|
||||
.append("\n\t访问地址:http://")
|
||||
.append(urlCtx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.zyplayer.doc.manage.framework.console;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* 启动后打印信息接口
|
||||
*
|
||||
* @author Sh1yu
|
||||
* @since 2023年6月16日
|
||||
*/
|
||||
public interface IConsolePrint extends Ordered {
|
||||
public void buildPrintInfo(StringBuffer printInfo) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.zyplayer.doc.manage.framework.console;
|
||||
|
||||
import com.zyplayer.doc.manage.framework.config.ZyplayerMoudleKeeper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 启动后打印模块信息
|
||||
*
|
||||
* @author Sh1yu
|
||||
* @since 2023年6月16日
|
||||
*/
|
||||
@Component
|
||||
public class MoudleInfoConsolePrint implements IConsolePrint {
|
||||
@Resource
|
||||
ZyplayerMoudleKeeper moudleKeeper;
|
||||
|
||||
@Override
|
||||
public void buildPrintInfo(StringBuffer printInfo) throws Exception {
|
||||
printInfo.append("\n\n\t\t\t\t↓zyplayer-doc模块的启动情况\n")
|
||||
.append("\t\t\t\t------------------------\n");
|
||||
HashMap<String, Boolean> moudleInfos = moudleKeeper.getMoudleInfo();
|
||||
for (Map.Entry<String, Boolean> moudleInfo : moudleInfos.entrySet()) {
|
||||
printInfo.append(getPerfectPosString(moudleInfo.getKey()))
|
||||
.append("模块启动情况为:")
|
||||
.append(false == moudleInfo.getValue() ? "未启动\n" : "启动成功\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getPerfectPosString(String beforeString) {
|
||||
final int pointLeft = 19;
|
||||
String afterOptin = beforeString.replace("enable", "");
|
||||
int length = afterOptin.length();
|
||||
int rightOffset = pointLeft - length;
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
for (int i = 0; i < rightOffset; i++) {
|
||||
stringBuffer.append(" ");
|
||||
}
|
||||
stringBuffer.append(afterOptin);
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.zyplayer.doc.manage.framework.console;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 程序启动后内容打印,新增打印内容只需要继承IConsolePrint
|
||||
* @author 暮光:城中城
|
||||
* @author Sh1yu
|
||||
* @since 2023年6月15日
|
||||
* @See IConsolePrint
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class ZyplayerConsolePrint implements CommandLineRunner {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ZyplayerConsolePrint.class);
|
||||
StringBuffer logInfoHolder = new StringBuffer();
|
||||
|
||||
@Autowired
|
||||
ObjectProvider<List<IConsolePrint>> print;
|
||||
|
||||
public void run(String... args) throws Exception {
|
||||
if (logger.isInfoEnabled()) {
|
||||
List<IConsolePrint> prints = print.getIfAvailable();
|
||||
if (prints.size() < 1) {
|
||||
return;
|
||||
}
|
||||
logInfoHolder.append("\n--------------------------------------------------------------\n\t");
|
||||
List<IConsolePrint> collect = prints.stream().sorted((a, b) -> {
|
||||
int aOrder = a.getOrder();
|
||||
int bOrder = b.getOrder();
|
||||
if (aOrder > bOrder) {
|
||||
return 1;
|
||||
}
|
||||
if (aOrder < bOrder) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}).collect(Collectors.toList());
|
||||
for (IConsolePrint consolePrint : collect) {
|
||||
consolePrint.buildPrintInfo(logInfoHolder);
|
||||
}
|
||||
logInfoHolder.append("--------------------------------------------------------------\n\t");
|
||||
logger.info(logInfoHolder.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.zyplayer.doc.manage.framework.interceptor;
|
||||
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.manage.framework.config.ZyplayerDocConfig;
|
||||
import com.zyplayer.doc.manage.framework.config.ZyplayerMoudleKeeper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -22,21 +23,21 @@ public class MoudleMissingInterceptor implements HandlerInterceptor {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MoudleMissingInterceptor.class);
|
||||
|
||||
@Resource
|
||||
ZyplayerDocConfig zyplayerDocConfig;
|
||||
ZyplayerMoudleKeeper moudleKeeper;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
String requestURI = request.getRequestURI();
|
||||
String simpleMoudleUri = requestURI.replace("/zyplayer-doc/", "");
|
||||
if (simpleMoudleUri.startsWith("zyplayer-doc-wiki") && !zyplayerDocConfig.isEnableWiki()) {
|
||||
if (simpleMoudleUri.startsWith("zyplayer-doc-wiki") && !moudleKeeper.isMoudleStarted(ZyplayerDocConfig.enableWiki.class)) {
|
||||
doFailResponse(response, "wiki模块未启动,无法提供相应功能");
|
||||
return false;
|
||||
}
|
||||
if (simpleMoudleUri.startsWith("zyplayer-doc-db") && !zyplayerDocConfig.isEnableDb()) {
|
||||
if (simpleMoudleUri.startsWith("zyplayer-doc-db") && !moudleKeeper.isMoudleStarted(ZyplayerDocConfig.enableDb.class)) {
|
||||
doFailResponse(response, "db模块未启动,无法提供相应功能");
|
||||
return false;
|
||||
}
|
||||
if (simpleMoudleUri.startsWith("zyplayer-doc-api") && !zyplayerDocConfig.isEnableApi()) {
|
||||
if (simpleMoudleUri.startsWith("zyplayer-doc-api") && !moudleKeeper.isMoudleStarted(ZyplayerDocConfig.enableApi.class)) {
|
||||
doFailResponse(response, "api模块未启动,无法提供相应功能");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.zyplayer.doc.manage.task;
|
||||
package com.zyplayer.doc.manage.framework.upgrade;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.zyplayer.doc.manage.task;
|
||||
package com.zyplayer.doc.manage.framework.upgrade;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.zyplayer.doc.manage.task;
|
||||
package com.zyplayer.doc.manage.framework.upgrade;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.zyplayer.doc.manage.task;
|
||||
package com.zyplayer.doc.manage.framework.upgrade;
|
||||
|
||||
import com.zyplayer.doc.core.util.ZyplayerDocVersion;
|
||||
|
||||
@@ -3,7 +3,8 @@ package com.zyplayer.doc.manage.web;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.manage.framework.config.ZyplayerDocConfig;
|
||||
import com.zyplayer.doc.manage.task.UpgradeUtil;
|
||||
import com.zyplayer.doc.manage.framework.config.ZyplayerMoudleKeeper;
|
||||
import com.zyplayer.doc.manage.framework.upgrade.UpgradeUtil;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -23,7 +24,7 @@ import javax.annotation.Resource;
|
||||
public class SystemInfoController {
|
||||
|
||||
@Resource
|
||||
ZyplayerDocConfig zyplayerDocConfig;
|
||||
ZyplayerMoudleKeeper moudleKeeper;
|
||||
|
||||
@PostMapping("/upgrade")
|
||||
public ResponseJson<Object> upgradeInfo() {
|
||||
@@ -32,7 +33,7 @@ public class SystemInfoController {
|
||||
|
||||
@GetMapping("/moudle")
|
||||
public ResponseJson<Object> moudleInfo() {
|
||||
return DocResponseJson.ok(zyplayerDocConfig.getMoudleInfo());
|
||||
return DocResponseJson.ok(moudleKeeper.getMoudleInfo());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user