【发布时间】:2020-08-09 13:37:41
【问题描述】:
我正在为我的大学学士论文开发一个网络应用程序。
我遇到了来自 Tomcat 的非常令人不安的行为。尽管我修复了迄今为止在真正的 Tomcat 9.0.34(最新版本 9)和嵌入式 Tomcat 9.0.31 上从“spring-boot-starter-tomcat”依赖项运行 WAR 的所有问题,但这个问题对我来说很神秘。
当 WAR 部署到 Tomcat 服务器时,没有任何代码(我也尝试过 SpringApplication.run(...) 行之后的 System.Out.println(...) 或 logger.info(...)。
代码在使用嵌入式 Tomcat 容器运行时执行(例如通过java -jar app.war)
在我的 pom.xml maven 设置中,Tomcat 在“运行时”范围内被标记,因此真实和嵌入式 Tomcat 选项都可用于 WAR。
我的应用程序通过这个 main 运行:
@SpringBootApplication
@ComponentScan(basePackages={"com.me.myProject"})
@ServletComponentScan
public class MySpringWebApplication extends SpringBootServletInitializer /* needed for WAR packaging */ {
...public static void main(String[] args){
ApplicationContext ctx = SpringApplication.run(MySpringWebApplication.class, args);
DatabaseSeeder sampleDataSeeder = new DatabaseSeeder(ctx.getBean(UserDAO.class), ctx.getBean(RegisterService.class), ctx.getBean(LibraryDAO.class));
sampleDataSeeder.ensureSampleLibraryExistence(); //this code is not executed in real Tomcat
sampleDataSeeder.ensureSampleLibraryExistence(); //this code isn't either
sampleDataSeeder.ensureSampleLibraryExistence(); //I spammed more of this to checkthat it really didn't happen
sampleDataSeeder.ensureSampleLibraryExistence();
sampleDataSeeder.ensureSampleLibraryExistence();
sampleDataSeeder.ensureSampleLibraryExistence();
EmailService service = ctx.getBean(EmailService.class);
service.sendEmailToMaintainer("end of main reached", "some text"); //this email is never sent on real Tomcat but always sent on embedded Tomcat
}}
我无法在 spring 上下文初始化之前为数据库数据播种,因为它会初始化我的服务等。
您知道那里可能出现什么情况吗? 谢谢你的建议!
一月
【问题讨论】:
标签: java spring-boot maven tomcat runtime