简体   繁体   English

如何在外部 Tomcat 服务器中运行 WAR 文件的设置代码

[英]How to run setup code for WAR file in External Tomcat Server

I have a spring boot application I'm building, and at the start, I need to check some system files and prepare some database pools using the information the app finds there.我有一个正在构建的 Spring Boot 应用程序,在开始时,我需要检查一些系统文件并使用应用程序在那里找到的信息准备一些数据库池。 Normally, I'd include this in the main method of the @SpringBootApplication annotated class, however, when I deploy my app as a WAR file to an external Tomcat server, that main class doesn't seem to run.通常,我@SpringBootApplication包含在@SpringBootApplication注释类的 main 方法中,但是,当我将我的应用程序作为 WAR 文件部署到外部 Tomcat 服务器时,该主类似乎没有运行。 I've checked around at what you're supposed to have in that main class, and my main application class now looks like this:我已经检查了你应该在那个主类中拥有的东西,我的主应用程序类现在看起来像这样:

package com.companyname.projectname;

import com.companyname.projectname.database.DatabasePoolManager;
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;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {

    private static final Logger logger = LoggerFactory.getLogger(WebApplication.class);

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(WebApplication.class, args);
        DatabasePoolManager dpm = applicationContext.getBean(DatabasePoolManager.class);
        dpm.setUpPools();
        logger.error("\n\nIS ANYBODY OUT THERE?\n\n");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        logger.error("\n\nIS ANYBODY OUT THERE? (But in the configure method)\n\n");
        return builder.sources(WebApplication.class);
    }

}

This is different than my original setup because of the extends and override of configure .由于configureextends和覆盖,这与我的原始设置不同。

So far, this still runs fine with my Intellij IDE, but once moved and deployed to the tomcat server, none of the log messages appear.到目前为止,这在我的 Intellij IDE 上仍然运行良好,但是一旦移动并部署到 tomcat 服务器,就不会出现任何日志消息。 The app still works, but is clearly missing some setup that grants it's functionality (connections to databases).该应用程序仍然有效,但显然缺少一些授予其功能的设置(与数据库的连接)。 How would I go about running some setup code on the application start, when I deploy this app as a WAR file?当我将此应用程序部署为 WAR 文件时,我将如何在应用程序启动时运行一些设置代码?

Thanks again to M. Deinum in the comments above, to run once on startup, I used this new class shown below:再次感谢 M. Deinum 在上面的评论中,为了在启动时运行一次,我使用了如下所示的新类:

package com.companyname.projectname;

import com.companyname.projectname.database.DatabasePoolManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class AppStartupRunner implements ApplicationRunner {

    @Autowired
    ApplicationContext applicationContext;

    private static final Logger logger = LoggerFactory.getLogger(AppStartupRunner.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        DatabasePoolManager dpm = applicationContext.getBean(DatabasePoolManager.class);
        dpm.setUpPools();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM