example.zip是一个非常简单的项目,我将其部署到独立的tomcat参考https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.traditional-deployment
有2个问题:
@PreDestroy
由于日志系统过早关闭,因此未输出登录信息LoggingApplicationListener::onContextClosedEvent
- tomcat将等待10秒完成关闭
package example;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class MainApplication extends SpringBootServletInitializer {
private Logger logger = LoggerFactory.getLogger(getClass());
public static void main(String[] args) throws Exception {
SpringApplication.run(MainApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(getClass());
}
@PostConstruct
private void init() {
logger.warn("init: " + this);
System.out.println("init: " + this);
}
@PreDestroy
private void destroy() {
logger.warn("destroy: " + this);
System.out.println("destroy: " + this);
}
}
2021-09-15 11:24:35.615 INFO 27966 --- [ main] example.MainApplication : Starting MainApplication using Java 11.0.12 on MyiMac with PID 27966 (/Users/zhouyanming/apache-tomcat-9.0.53/webapps/example-1.0.0-SNAPSHOT/WEB-INF/classes started by zhouyanming in /Users/zhouyanming)
2021-09-15 11:24:35.618 INFO 27966 --- [ main] example.MainApplication : No active profile set, falling back to default profiles: default
2021-09-15 11:24:36.273 INFO 27966 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 607 ms
2021-09-15 11:24:36.418 WARN 27966 --- [ main] ication$$EnhancerBySpringCGLIB$$c8f12ab1 : init: example.MainApplication$$EnhancerBySpringCGLIB$$c8f12ab1@1806bc4c
init: example.MainApplication$$EnhancerBySpringCGLIB$$c8f12ab1@1806bc4c
2021-09-15 11:24:36.766 INFO 27966 --- [ main] example.MainApplication : Started MainApplication in 1.596 seconds (JVM running for 2.629)
15-Sep-2021 11:24:36.795 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/Users/zhouyanming/apache-tomcat-9.0/webapps/example-1.0.0-SNAPSHOT.war] has finished in [2,004] ms
15-Sep-2021 11:24:36.798 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
15-Sep-2021 11:24:36.811 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [2051] milliseconds
^C15-Sep-2021 11:24:41.365 INFO [Thread-3] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
15-Sep-2021 11:24:41.373 INFO [Thread-3] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina]
destroy: example.MainApplication$$EnhancerBySpringCGLIB$$c8f12ab1@1806bc4c
15-Sep-2021 11:24:41.514 INFO [Thread-3] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
15-Sep-2021 11:24:51.520 WARNING [Thread-3] org.apache.tomcat.util.net.Acceptor.stop The acceptor thread [http-nio-8080-Acceptor] did not stop cleanly
15-Sep-2021 11:24:51.525 INFO [Thread-3] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]