SpringBoot2.x ApplicationLIsteners

目录

ApplicationLIsteners 

 应用


ApplicationLIsteners 

In addition to the usual Spring Framework events, such as ContextRefreshedEvent, a SpringApplication sends some additional application events.

Some events are actually triggered before the ApplicationContext is created, so you cannot register a listener on those as a @Bean. You can register them with the SpringApplication.addListeners(…​) method or the SpringApplicationBuilder.listeners(…​) method.

If you want those listeners to be registered automatically, regardless of the way the application is created, you can add a META-INF/spring.factoriesfile to your project and reference your listener(s) by using the org.springframework.context.ApplicationListener key, as shown in the following example:

org.springframework.context.ApplicationListener=com.example.project.MyListener

Application events are sent in the following order, as your application runs:

  1. An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
  2. An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
  3. An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
  4. An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
  5. An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.
  6. An ApplicationFailedEvent is sent if there is an exception on startup.

You often need not use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks.

Application events are sent by using Spring Framework’s event publishing mechanism. Part of this mechanism ensures that an event published to the listeners in a child context is also published to the listeners in any ancestor contexts. As a result of this, if your application uses a hierarchy of SpringApplication instances, a listener may receive multiple instances of the same type of application event.

To allow your listener to distinguish between an event for its context and an event for a descendant context, it should request that its application context is injected and then compare the injected context with the context of the event. The context can be injected by implementing ApplicationContextAware or, if the listener is a bean, by using @Autowired.

 某些事件实际上ApplicationContext是在创建之前触发的,因此您无法在这些事件上注册一个侦听器@Bean

SpringBoot2.x ApplicationLIsteners_第1张图片

 应用

在SpringBoot项目启动时 做一些事

1.创建一个实现了ApplicationListener的实现类

public class SLTMain implements ApplicationListener {

	private static final Logger logger = LoggerFactory.getLogger(SLTMain.class);

	
	@Override
	public void onApplicationEvent(ApplicationStartedEvent event) {

		logger.info("ApplicationStartedEvent.............");
	}

}

2在resource资源目录META-INF创建一个spring.factories 

spring.factories 内容是

org.springframework.context.ApplicationListener=\
com.jccfc.activiti.web.config.SLTMain

这样做就能在SpringBoot项目启动的时候做一些事  但是不能够@Autowired

SpringBoot2.x ApplicationLIsteners_第2张图片

会出现空指针异常

 

参考博客:各个监听器的含义以及使用

你可能感兴趣的:(#,SpringBoot,面试)