Issue
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. 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. 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
.
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. 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?
Solution
Thanks again to M. Deinum in the comments above, to run once on startup, I used this new class shown below:
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();
}
}
Answered By - JAF