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: utorialspringbootmycodespringboot-24-01-dynamic-beans argetclassescomgomepayStartupOrder.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