From 5e4f58ff79044cd19030cc229c57806e2eeb12ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Fri, 27 Aug 2021 17:47:44 +0800 Subject: [PATCH] perf($Starter): destroy SpringBootStartupHelper after it finishes work refine SpringBootStartupHelper logs BREAKING CHANGE: refine SpringBootStartupHelper logs --- .../MafReactiveAutoConfiguration.java | 5 ++- .../helper/SpringBootStartupHelper.java | 37 ++++++++++++++----- .../MafAutoConfiguration.java | 5 ++- .../helper/SpringBootStartupHelper.java | 35 +++++++++++++----- 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/MafReactiveAutoConfiguration.java b/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/MafReactiveAutoConfiguration.java index 153c0b4a..bfa021f0 100644 --- a/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/MafReactiveAutoConfiguration.java +++ b/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/MafReactiveAutoConfiguration.java @@ -16,6 +16,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -62,9 +63,9 @@ public IpHelper ipHelper(MafProjectProperty mafProjectProperty) { @Bean public SpringBootStartupHelper springBootStartupHelper(MafProjectProperty mafProjectProperty, - IpHelper ipHelper) { + IpHelper ipHelper, ApplicationContext applicationContext) { log.warn("Initial bean: '{}'", SpringBootStartupHelper.class.getSimpleName()); - return new SpringBootStartupHelper(mafProjectProperty, ipHelper); + return new SpringBootStartupHelper(mafProjectProperty, ipHelper, applicationContext); } @Bean diff --git a/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/helper/SpringBootStartupHelper.java b/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/helper/SpringBootStartupHelper.java index 2b670582..4db9e0ae 100644 --- a/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/helper/SpringBootStartupHelper.java +++ b/reactive-spring-cloud-starter/src/main/java/com/jmsoftware/maf/reactivespringcloudstarter/helper/SpringBootStartupHelper.java @@ -1,9 +1,14 @@ package com.jmsoftware.maf.reactivespringcloudstarter.helper; +import cn.hutool.core.lang.Console; import com.jmsoftware.maf.reactivespringcloudstarter.configuration.MafProjectProperty; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; @@ -19,21 +24,33 @@ @Slf4j @Component @RequiredArgsConstructor -public class SpringBootStartupHelper { +public class SpringBootStartupHelper implements DisposableBean { private static final String LINE_SEPARATOR = System.lineSeparator(); private final MafProjectProperty mafProjectProperty; private final IpHelper ipHelper; + private final ApplicationContext applicationContext; public void stop(@NonNull StopWatch stopWatch) { stopWatch.stop(); - log.info("🥳 Congratulations! 🎉"); - log.info("🖥 {}@{} started!", mafProjectProperty.getProjectArtifactId(), mafProjectProperty.getVersion()); - log.info("⚙️ Environment: {}", mafProjectProperty.getEnvironment()); - log.info("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(), - stopWatch.getTotalTimeMillis()); - log.info("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")), TimeZone.getDefault().getDisplayName()); - log.info("{} App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/", - LINE_SEPARATOR, LINE_SEPARATOR, ipHelper.getServerPort(), mafProjectProperty.getContextPath(), - LINE_SEPARATOR, ipHelper.getPublicIp(), ipHelper.getServerPort(), mafProjectProperty.getContextPath()); + Console.log("🥳 Congratulations! 🎉"); + Console.log("🖥 {}@{} started!", this.mafProjectProperty.getProjectArtifactId(), + this.mafProjectProperty.getVersion()); + Console.log("⚙️ Environment: {}", this.mafProjectProperty.getEnvironment()); + Console.log("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(), + stopWatch.getTotalTimeMillis()); + Console.log("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")), + TimeZone.getDefault().getDisplayName()); + Console.error(" App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/", + LINE_SEPARATOR, this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath(), + LINE_SEPARATOR, this.ipHelper.getPublicIp(), this.ipHelper.getServerPort(), + this.mafProjectProperty.getContextPath()); + val defaultListableBeanFactory = + (DefaultListableBeanFactory) this.applicationContext.getAutowireCapableBeanFactory(); + defaultListableBeanFactory.destroyBean(this); + } + + @Override + public void destroy() { + log.warn("Destroyed bean: {}", this.getClass().getSimpleName()); } } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java index d999eeb6..e0db2735 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java @@ -31,6 +31,7 @@ import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -119,9 +120,9 @@ public IpHelper ipHelper(Environment environment) { @Bean public SpringBootStartupHelper springBootStartupHelper(MafProjectProperty mafProjectProperty, - IpHelper ipHelper) { + IpHelper ipHelper, ApplicationContext applicationContext) { log.warn("Initial bean: '{}'", SpringBootStartupHelper.class.getSimpleName()); - return new SpringBootStartupHelper(mafProjectProperty, ipHelper); + return new SpringBootStartupHelper(mafProjectProperty, ipHelper, applicationContext); } @Bean diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/helper/SpringBootStartupHelper.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/helper/SpringBootStartupHelper.java index 006e11cc..dd9e7cde 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/helper/SpringBootStartupHelper.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/helper/SpringBootStartupHelper.java @@ -1,9 +1,14 @@ package com.jmsoftware.maf.springcloudstarter.helper; +import cn.hutool.core.lang.Console; import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; @@ -19,21 +24,33 @@ @Slf4j @Component @RequiredArgsConstructor -public class SpringBootStartupHelper { +public class SpringBootStartupHelper implements DisposableBean { private static final String LINE_SEPARATOR = System.lineSeparator(); private final MafProjectProperty mafProjectProperty; private final IpHelper ipHelper; + private final ApplicationContext applicationContext; public void stop(@NonNull StopWatch stopWatch) { stopWatch.stop(); - log.info("🥳 Congratulations! 🎉"); - log.info("🖥 {}@{} started!", this.mafProjectProperty.getProjectArtifactId(), this.mafProjectProperty.getVersion()); - log.info("⚙️ Environment: {}", this.mafProjectProperty.getEnvironment()); - log.info("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(), + Console.log("🥳 Congratulations! 🎉"); + Console.log("🖥 {}@{} started!", this.mafProjectProperty.getProjectArtifactId(), + this.mafProjectProperty.getVersion()); + Console.log("⚙️ Environment: {}", this.mafProjectProperty.getEnvironment()); + Console.log("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(), stopWatch.getTotalTimeMillis()); - log.info("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")), TimeZone.getDefault().getDisplayName()); - log.info("{} App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/", - LINE_SEPARATOR, LINE_SEPARATOR, this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath(), - LINE_SEPARATOR, this.ipHelper.getPublicIp(), this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath()); + Console.log("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")), + TimeZone.getDefault().getDisplayName()); + Console.error(" App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/", + LINE_SEPARATOR, this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath(), + LINE_SEPARATOR, this.ipHelper.getPublicIp(), this.ipHelper.getServerPort(), + this.mafProjectProperty.getContextPath()); + val defaultListableBeanFactory = + (DefaultListableBeanFactory) this.applicationContext.getAutowireCapableBeanFactory(); + defaultListableBeanFactory.destroyBean(this); + } + + @Override + public void destroy() { + log.warn("Destroyed bean: {}", this.getClass().getSimpleName()); } }