First is the SpringApplication.run
method, which takes a primary source
and then parses and loads the configuration (which is the App.class passed in).
Then a SpringApplication
is created, which checks what type of project it is: this.webApplicationType = WebApplicationType.deduceFromClasspath();
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
The parameters are passed in, and then run()
is called to create a ConfigurableApplicationContext
based on the project type.
public ConfigurableApplicationContext run(String... args) {
// Start the timer
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
// This is used to manage multiple listeners. When created, it retrieves the names of the listeners to be created from META-INF/spring.factories and creates instances.
SpringApplicationRunListeners listeners = getRunListeners(args);
// Start the listener
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
// Create runtime arguments
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
// Output the Spring Boot banner
Banner printedBanner = printBanner(environment);
// Create the application context
context = createApplicationContext();
// Exception reporting
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
// Pre-configure the context
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
// Refresh the context
refreshContext(context);
afterRefresh(context, applicationArguments);
// Stop the timer
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
The listeners are used to create these things:
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
This means that we can customize listeners to listen to the corresponding events by implementing ApplicationListener
.
public class MyListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// Listen to ApplicationStartingEvent
if (event instanceof ApplicationStartedEvent) {
System.out.println("ApplicationStartedEvent listened");
}
// Listen to ApplicationEnvironmentPreparedEvent
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
System.out.println("ApplicationEnvironmentPreparedEvent listened");
}
// Listen to ApplicationPreparedEvent
else if (event instanceof ApplicationPreparedEvent) {
System.out.println("ApplicationPreparedEvent listened");
}
// Listen to ApplicationReadyEvent
else if (event instanceof ApplicationReadyEvent) {
System.out.println("ApplicationReadyEvent listened");
}
// Listen to ApplicationFailedEvent
else if (event instanceof ApplicationFailedEvent) {
System.out.println("ApplicationFailedEvent listened");
}
}
}
Add the following to META-INF/spring.factories
:
org.springframework.context.ApplicationListener=\
me.dqn.listener.MyListener
Or you can manually add it:
public static void main(String[] args) {
SpringApplication application = new SpringApplication(App.class);
application.addListeners(new MyListener());
application.run(args);
}