Several important event callback mechanism:
1 , arranged in the META-INF / spring.factories
ApplicationContextInitializer
SpringApplicationRunlistener
2 , needs to be placed ioc container
3、ApplicationRunner
4、CommandLineRunner
First, create SpringApplication objects
Quickly create a project, in SpringBootApplication class, main method as an entry class in the RUN () method at the marked breakpoint, Debug run;
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
// save the master configuration class
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// determine whether a current web application
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// find the path from the class system META-INF / spring.factories all configured ApplicationContextInitializer , then save it
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// find the path from the class system META-INF / spring.factories all configurations ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// find there are arranged a plurality of classes from a main master configuration class methods
this.mainApplicationClass = deduceMainApplicationClass();
}
Second, run run method
Back then continued:
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
// Get SpringApplicationRunListeners , from the classpath METTA-INF / spring.factories
SpringApplicationRunListeners listeners = getRunListeners(args);
// callback All SpringApplicationRunListeners.starting method
listeners.starting();
try {
// package the command line parameters
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// Prepare the environment
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
// create the environment after completion callback SpringApplicationRunListeners.environmentPrepared (); is the ambient ready
configureIgnoreBeanInfo(environment);
Banner = printedBanner print banner (environment);
// create applicationContext , decided to create a web of ioc or ordinary ioc
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
// prepare context; the environment is saved to ioc in; and applyInitializers ()
applyInitializers // () : All previously saved callback ApplicationContextInitializer the Initialize method
// callback All SpringApplicationRunListeners.contextPrepared ()
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// prepareContext run after completion callback all SpringApplicationRunListeners of contextLoaded ()
// refresh container; IOC container initialization (if a web application will create an embedded Tomcat ); the Spring comment
// scan, create, load places all components; (configuration classes, components, auto-configuration)
refreshContext(context);
// From ioc get all containers ApplicationRunner and CommandLineRunner callback
// ApplicationRunner first callback, CommandLineRunner again callback
afterRefresh(context, applicationArguments);
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);
}
// entire SpringBoot application startup is complete forex rebate after the start of the return ioc container
return context;
} Third, the event listener mechanism
Turn the monitor above several mechanisms to create,
HelloApplicationContextInitializer:
public class HelloApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("ApplicationContextInitializer...initialize..." + applicationContext);
}
}
HelloSpringApplicationRunListener:
public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {
public HelloSpringApplicationRunListener(SpringApplication application, String[] args){
}
@Override
public void starting() {
System.out.println("SpringApplicationRunListener...starting...");
}
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
Object o = environment.getSystemProperties().get("os.name");
System.out.println("SpringApplicationRunListener...environment" +o);
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...contextPrepared");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...contextLoaded");
}
@Override
public void started(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...started");
}
@Override
public void running(ConfigurableApplicationContext context) {
System.out.println("SpringApplicationRunListener...running");
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("SpringApplicationRunListener...failed");
}
}
HelloApplicationRunner:
@Component
public class HelloAppicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner...run..");
}
}
HelloCommandLineRuinner :
@Component
public class HelloCommandLineRunner implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner...run..." + Arrays.asList());
}
}
The aforementioned ApplicationContextInitializer and SpringApplicationRunlistener need configuring, is disposed at a position in the META-INF / spring.factories , so create a META-INF folder, and create spring.factories file,
org.springframework.context.ApplicationContextInitializer=\
com.itlaoqi.springbootspringbootapplication.listener.HelloApplicationContextInitializer
org.springframework.context.SpringApplicationRunListener=\
com.itlaoqi.springbootspringbootapplication.listener.HelloSpringApplicationRunListener
Start the project can be tested after configuration is complete.
Original link: https://blog.csdn.net/cyl101816/article/details/103582373