SpringBoot应用启动与回调监听机制如何解析?

2026-06-10 15:46:27 852阅读 0评论 SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计890个文字,预计阅读时间需要4分钟。

这篇文章主要介绍了SpringBoot启动应用及回调监听原理,通过示例代码展示了其非详细的实现。对于想要深入学习或工作的朋友,具有一定的参考价值。以下是关键点:

SpringBoot应用启动与回调监听机制如何解析?

1. SpringBoot启动应用原理

2.回调监听机制解析

3.示例代码展示

SpringBoot应用启动与回调监听机制如何解析?

1. SpringBoot启动应用原理

SpringBoot通过简化Spring应用的创建和配置,让开发者可以更专注于业务逻辑。其启动流程如下:

- 解析启动类上的`@SpringBootApplication`注解

- 加载配置文件- 创建Spring容器- 执行`@Bean`注解的方法,初始化Bean- 启动应用

2. 回调监听机制解析

SpringBoot提供了丰富的回调监听机制,如`@PostConstruct`、`@PreDestroy`等注解。这些注解可以让开发者自定义在Bean初始化前后执行的代码。

3. 示例代码展示

以下是一个简单的示例,展示了SpringBoot启动应用和回调监听:

javaimport org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.context.event.EventListener;

@SpringBootApplicationpublic class Application {

public static void main(String[] args) { SpringApplication.run(Application.class, args); }

@Bean public String hello() { return Hello, World!; }

@EventListener public void handleContextRefresh(ContextRefreshedEvent event) { System.out.println(Context refreshed); }}

在上述代码中,`@SpringBootApplication`注解表示这是一个SpringBoot应用,`@Bean`注解的方法`hello()`用于创建一个名为`hello`的Bean,`@EventListener`注解的方法`handleContextRefresh()`用于监听`ContextRefreshedEvent`事件,在应用启动后执行。

总结

SpringBoot为开发者提供了便捷的启动和应用管理方式,其回调监听机制也使得开发者可以轻松地实现自定义逻辑。希望本文能对您有所帮助。

这篇文章主要介绍了SpringBoot启动应用及回调监听原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

主类Main方法

public static void main(String[] args) { SpringApplication.run(SpringBootRunApplication.class, args); }

创建SpringApplication对象

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args); }  

构造器

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.sources = new LinkedHashSet(); this.bannerMode = Mode.CONSOLE; this.logStartupInfo = true; this.addCommandLineProperties = true; this.addConversionService = true; this.headless = true; this.registerShutdownHook = true; this.additionalProfiles = new HashSet(); this.isCustomEnvironment = false; this.lazyInitialization = false; this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); this.webApplicationType = WebApplicationType.deduceFromClasspath(); this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = this.deduceMainApplicationClass(); }

ApplicationContextInitializer&ApplicationListener

读取META-INF/spring.factories文件中的类 

this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));

执行run方法 

public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); // IOC容器 ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList(); this.configureHeadlessProperty(); // 读取META-INF/spring.factories文件获得SpringApplicationRunListener的实现类集合 SpringApplicationRunListeners listeners = this.getRunListeners(args); // 监听开始,回调所有SpringApplicationRunListener的starting()方法 listeners.starting(); Collection exceptionReporters; try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 监听配置环境准备好了,回调所有SpringApplicationRunListener的environmentPrepared()方法 ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); this.configureIgnoreBeanInfo(environment); Banner printedBanner = this.printBanner(environment); // 创建IOC容器 context = this.createApplicationContext(); exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context); // 回调ApplicationContextInitializer的initialize()方法,回调SpringApplicationRunListener的contextPrepared()方法 // 回调SpringApplicationRunListener的contextLoaded()方法 this.prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 刷新IOC容器,注入组件 this.refreshContext(context); this.afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch); } // 回调SpringApplicationRunListener的started()方法 listeners.started(context); // 从IOC容器中获得ApplicationRunner、CommandLineRunner的所有组件,回调ApplicationRunner、CommandLineRunner的run()方法 this.callRunners(context, applicationArguments); } catch (Throwable var10) { this.handleRunFailure(context, var10, exceptionReporters, listeners); throw new IllegalStateException(var10); } try { // 回调SpringApplicationRunListener的running()方法 listeners.running(context); return context; } catch (Throwable var9) { this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null); throw new IllegalStateException(var9); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计890个文字,预计阅读时间需要4分钟。

这篇文章主要介绍了SpringBoot启动应用及回调监听原理,通过示例代码展示了其非详细的实现。对于想要深入学习或工作的朋友,具有一定的参考价值。以下是关键点:

SpringBoot应用启动与回调监听机制如何解析?

1. SpringBoot启动应用原理

2.回调监听机制解析

3.示例代码展示

SpringBoot应用启动与回调监听机制如何解析?

1. SpringBoot启动应用原理

SpringBoot通过简化Spring应用的创建和配置,让开发者可以更专注于业务逻辑。其启动流程如下:

- 解析启动类上的`@SpringBootApplication`注解

- 加载配置文件- 创建Spring容器- 执行`@Bean`注解的方法,初始化Bean- 启动应用

2. 回调监听机制解析

SpringBoot提供了丰富的回调监听机制,如`@PostConstruct`、`@PreDestroy`等注解。这些注解可以让开发者自定义在Bean初始化前后执行的代码。

3. 示例代码展示

以下是一个简单的示例,展示了SpringBoot启动应用和回调监听:

javaimport org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.context.event.EventListener;

@SpringBootApplicationpublic class Application {

public static void main(String[] args) { SpringApplication.run(Application.class, args); }

@Bean public String hello() { return Hello, World!; }

@EventListener public void handleContextRefresh(ContextRefreshedEvent event) { System.out.println(Context refreshed); }}

在上述代码中,`@SpringBootApplication`注解表示这是一个SpringBoot应用,`@Bean`注解的方法`hello()`用于创建一个名为`hello`的Bean,`@EventListener`注解的方法`handleContextRefresh()`用于监听`ContextRefreshedEvent`事件,在应用启动后执行。

总结

SpringBoot为开发者提供了便捷的启动和应用管理方式,其回调监听机制也使得开发者可以轻松地实现自定义逻辑。希望本文能对您有所帮助。

这篇文章主要介绍了SpringBoot启动应用及回调监听原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

主类Main方法

public static void main(String[] args) { SpringApplication.run(SpringBootRunApplication.class, args); }

创建SpringApplication对象

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args); }  

构造器

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { this.sources = new LinkedHashSet(); this.bannerMode = Mode.CONSOLE; this.logStartupInfo = true; this.addCommandLineProperties = true; this.addConversionService = true; this.headless = true; this.registerShutdownHook = true; this.additionalProfiles = new HashSet(); this.isCustomEnvironment = false; this.lazyInitialization = false; this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); this.webApplicationType = WebApplicationType.deduceFromClasspath(); this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = this.deduceMainApplicationClass(); }

ApplicationContextInitializer&ApplicationListener

读取META-INF/spring.factories文件中的类 

this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));

执行run方法 

public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); // IOC容器 ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList(); this.configureHeadlessProperty(); // 读取META-INF/spring.factories文件获得SpringApplicationRunListener的实现类集合 SpringApplicationRunListeners listeners = this.getRunListeners(args); // 监听开始,回调所有SpringApplicationRunListener的starting()方法 listeners.starting(); Collection exceptionReporters; try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 监听配置环境准备好了,回调所有SpringApplicationRunListener的environmentPrepared()方法 ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); this.configureIgnoreBeanInfo(environment); Banner printedBanner = this.printBanner(environment); // 创建IOC容器 context = this.createApplicationContext(); exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context); // 回调ApplicationContextInitializer的initialize()方法,回调SpringApplicationRunListener的contextPrepared()方法 // 回调SpringApplicationRunListener的contextLoaded()方法 this.prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 刷新IOC容器,注入组件 this.refreshContext(context); this.afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch); } // 回调SpringApplicationRunListener的started()方法 listeners.started(context); // 从IOC容器中获得ApplicationRunner、CommandLineRunner的所有组件,回调ApplicationRunner、CommandLineRunner的run()方法 this.callRunners(context, applicationArguments); } catch (Throwable var10) { this.handleRunFailure(context, var10, exceptionReporters, listeners); throw new IllegalStateException(var10); } try { // 回调SpringApplicationRunListener的running()方法 listeners.running(context); return context; } catch (Throwable var9) { this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null); throw new IllegalStateException(var9); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。