1、MytestApplication

package com.gomepay;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class MytestApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MytestApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.addListeners(new ApplicationStartup());
app.run(args);
}
}

2、StartupOrder

package com.gomepay;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.ServletContextAware; import javax.annotation.PostConstruct;
import javax.servlet.ServletContext; @Configuration
public class StartupOrder {
@Bean
static BeanDefinitionRegistryPostProcessor beanPostProcessor(final ConfigurableEnvironment environment) {
return new BeanDefinitionRegistryPostProcessor() {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("======1.BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("======2.BeanDefinitionRegistryPostProcessor.postProcessBeanFactory()");
}
};
}
@PostConstruct
private void init() {
System.out.println("======3.@PostConstruct");
}
@Bean
static ServletContextAware servletContextAware(final ConfigurableEnvironment environment) {
return new ServletContextAware() {
@Override
public void setServletContext(ServletContext servletContext) {
System.out.println("======4.ServletContextAware.setServletContext()");
}
};
}
@Bean
static BeanNameAware beanNameAware(final ConfigurableEnvironment environment) {
return new BeanNameAware() {
@Override
public void setBeanName(String name) {
environment.getProperty("app.info");
System.out.println("======5.BeanNameAware.setBeanName()");
}
};
}
@Bean
static ApplicationContextAware aware(final ConfigurableEnvironment environment) {
return new ApplicationContextAware() {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("======6.ApplicationContextAware.setApplicationContext()");
}
};
}
@Bean
static InitializingBean initBean(final ConfigurableEnvironment environment) {
return new InitializingBean() {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("======7.InitializingBean.afterPropertiesSet()");
}
};
} /**
* 在tomcat启动成功时
* @param event
*/
@EventListener(WebServerInitializedEvent.class)
public void onWebServerReady(WebServerInitializedEvent event) {
System.out.println("======9.@EventListener(WebServerInitializedEvent.class)");
}
/**
* 在spring boot应用启动后回调
* @param context
* @return
*/
@Bean
public ApplicationRunner runner(WebServerApplicationContext context) {
return args -> {
System.out.println("======10.ApplicationRunner");
};
}
} class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext ac = event.getApplicationContext();
//User user = ac.getBean(User.class);
System.out.println("======8.ApplicationListener.onApplicationEvent()");
}
}

输出日志:

13:57:38.764 DEBUG | Loading source class com.gomepay.MytestApplication
13:57:38.813 DEBUG | Activated activeProfiles prod
13:57:38.813 DEBUG | Loaded config file 'file:/D:/tutorial/springboot/mycode/springboot-24-01-dynamic-beans/target/classes/application.yml' (classpath:/application.yml)
13:57:38.813 DEBUG | Loaded config file 'file:/D:/tutorial/springboot/mycode/springboot-24-01-dynamic-beans/target/classes/application-prod.yml' (classpath:/application-prod.yml) for profile prod
13:57:38.814 DEBUG | Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@662b4c69
13:57:38.834 DEBUG | Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
13:57:38.844 DEBUG | Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'
13:57:38.899 DEBUG | Identified candidate component class: file [D:\tutorial\springboot\mycode\springboot-24-01-dynamic-beans\target\classes\com\gomepay\StartupOrder.class]
13:57:39.040 DEBUG | Found key 'spring.application.admin.enabled' in PropertySource 'configurationProperties' with value of type String
13:57:39.208 DEBUG | Found key 'spring.application.admin.enabled' in PropertySource 'configurationProperties' with value of type String
13:57:39.257 DEBUG | Creating shared instance of singleton bean 'beanPostProcessor'
13:57:39.263 DEBUG | Autowiring by type from bean name 'beanPostProcessor' via factory method to bean named 'environment'
======1.BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()
======2.BeanDefinitionRegistryPostProcessor.postProcessBeanFactory()

13:57:39.334 DEBUG | Creating shared instance of singleton bean 'propertySourcesPlaceholderConfigurer'
13:57:39.337 DEBUG | Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator'

13:57:39.464 DEBUG | Autowiring by type from bean name 'servletWebServerFactoryCustomizer' via factory method to bean named 'server-org.springframework.boot.autoconfigure.web.ServerProperties'

13:57:39.637 INFO | Tomcat initialized with port(s): 8080 (http)
13:57:39.647 INFO | Initializing ProtocolHandler ["http-nio-8080"]
13:57:39.648 INFO | Starting service [Tomcat]

13:57:39.806 DEBUG | Mapping servlets: dispatcherServlet urls=[/]
13:57:39.822 DEBUG | Filter 'requestContextFilter' configured for use
13:57:39.822 DEBUG | Filter 'characterEncodingFilter' configured for use
13:57:39.822 DEBUG | Filter 'formContentFilter' configured for use
13:57:39.831 DEBUG | Creating shared instance of singleton bean 'mytestApplication'
13:57:39.832 DEBUG | Creating shared instance of singleton bean 'startupOrder'
======3.@PostConstruct
13:57:39.833 DEBUG | Creating shared instance of singleton bean 'servletContextAware'
13:57:39.833 DEBUG | Autowiring by type from bean name 'servletContextAware' via factory method to bean named 'environment'
======4.ServletContextAware.setServletContext()
13:57:39.834 DEBUG | Creating shared instance of singleton bean 'beanNameAware'
13:57:39.834 DEBUG | Autowiring by type from bean name 'beanNameAware' via factory method to bean named 'environment'
13:57:39.835 DEBUG | Found key 'app.info' in PropertySource 'configurationProperties' with value of type String
======5.BeanNameAware.setBeanName()
13:57:39.836 DEBUG | Creating shared instance of singleton bean 'aware'
13:57:39.836 DEBUG | Autowiring by type from bean name 'aware' via factory method to bean named 'environment'
======6.ApplicationContextAware.setApplicationContext()
13:57:39.837 DEBUG | Creating shared instance of singleton bean 'initBean'
13:57:39.837 DEBUG | Autowiring by type from bean name 'initBean' via factory method to bean named 'environment'
======7.InitializingBean.afterPropertiesSet()
13:57:39.838 DEBUG | Creating shared instance of singleton bean 'runner'

。。。

======8.ApplicationListener.onApplicationEvent()
13:57:40.147 DEBUG | Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String
13:57:40.147 INFO | Starting ProtocolHandler ["http-nio-8080"]
13:57:40.170 INFO | Tomcat started on port(s): 8080 (http) with context path ''
======9.@EventListener(WebServerInitializedEvent.class)
13:57:40.173 INFO | Started MytestApplication in 1.852 seconds (JVM running for 4.396)
======10.ApplicationRunner
13:57:40.458 DEBUG | Found key 'local.server.port' in PropertySource 'server.ports' with value of type Integer

spring boot 生命周期初探的更多相关文章

  1. Spring的生命周期

    转:https://blog.csdn.net/liuxilil/article/details/4676088 Spring的生命周期. 容器启动,实例化所有实现了BeanFactoyPostPro ...

  2. spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理

    1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...

  3. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

  4. spring之生命周期

    1.容器中对的生命周期 spring可以管理 singleton作用域的bean的生命周期,spring可以精确地知道该bean何时被创建,何时被初始化完成,容器合适准备销毁该bean实例. spri ...

  5. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  6. 常见问题:Web/Servlet生命周期与Spring Bean生命周期

    Servlet生命周期 init()初始化阶段 Servlet容器加载Servlet(web.xml中有load-on-startup=1;Servlet容器启动后用户首次向Servlet发请求;Se ...

  7. spring的后置处理器——BeanPostProcessor以及spring的生命周期

    后置处理器的调用时机 BeanPostProcessor是spring提供的接口,它有两个方法——postProcessBeforeInitialization.postProcessAfterIni ...

  8. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  9. Spring Bean生命周期,好像人的一生。。

    大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...

随机推荐

  1. MQTT v5 (MQTT 5.0) 新特性介绍

    https://blog.csdn.net/mrpre/article/details/87267400 背景 MQTT v3.1.1 作为一个经典的版本,一般能够满足大部分需求:为了避免落后,我们也 ...

  2. SpringBoot#ConfigurationProperties注解相关的一些知识

    用途:ConfigurationProperties注解,用于在spring环境定义bean的时候.通过这个注解,把配置文件中的相关属性注入到实例化的bean中. 原理:spring中bean的生命周 ...

  3. 桌面右键没有新建txt文本文档的快捷方式、

    先新建一个word文档,然后将后缀名改为 TXT. 在里面输入:Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.txt] @=&quo ...

  4. 谈谈对MapTask任务分配和Shuffle的理解

    一.切片与MapTask的关系 1.概述 大家要注意区分切片与切块的区别: 切块Block是HDFS物理上把数据分成一块一块的,默认是128M: 数据切片:只是在逻辑上对输入进行分片,并不会在磁盘上分 ...

  5. 转 centos7使用kubeadm安装kubernetes 1.12版本

    最近在学习go 模板语法  还是有点小忙 !   感觉写这种 kuberadm 搭建 kubernetes  还是有点水  好吧 可能我太高调了  前前后后搭过四 五次了  ! kuberadm 搭建 ...

  6. Day 23:JAVA SE复习

    作业 1.多线程下载图片 import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream ...

  7. 对上一篇Logstash的补充

    主要补充内容: 1.同步多表 2.配置的参数个别说明 3.elasticsearch的"_id"如果有相同的,那么会覆盖掉,相同"_id"的数据只会剩下最后一条 ...

  8. 【LeetCode】101. 对称二叉树

    题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...

  9. PLC与单片机执行指令区别

    单片机执行指令方式与PLC执行指令方式对比 . 映射 对顺序功能图并行分支的理解.   PLC与单片机都是顺序执行指令方式的. PLC执行指令分为3个阶段. PLC的一个指令周期包括 输入采样 程序执 ...

  10. Redis 详解 (八) 主从复制

    目录 1.修改配置文件 2.设置主从关系 3.测试主从关系 4.哨兵模式 5.主从复制原理 6.主从复制的缺点 前面介绍Redis,我们都在一台服务器上进行操作的,也就是说读和写以及备份操作都是在一台 ...