Spring ApplicationListener is not receiving events

46,844

Solution 1

ContextStartedEvent is published when you explicitly invoke ConfigurableApplicationContext.start() on the context. If you need an event that is published when context is initialized, use ContextRefreshedEvent.

See also:

Solution 2

Since you have no lazy loaded beans (according to you) then you are most likely using events for the wrong reason and probably should use something like InitializingBean interface instead:

public class MyBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // ...
    }

}

From Spring manual:

To interact with the container's management of the bean lifecycle, you can implement the Spring InitializingBean and DisposableBean interfaces. The container calls afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction of your beans. You can also achieve the same integration with the container without coupling your classes to Spring interfaces through the use of init-method and destroy method object definition metadata.

Source: Spring Framework - Lifecycle callbacks

Share:
46,844

Related videos on Youtube

Andrey Adamovich
Author by

Andrey Adamovich

A software craftsman with many years of experience in different lifecycle phases of software creation. He is passionate about defining good development practices, documenting and presenting architecture, reuse of code and design patterns, profiling and analysis of application performance as well as extreme automation of development and operations activities. At the moment, Andrey is working as a free-lance DevOps consultant offering his expertise in implementing DevOps initiatives, selecting automation tooling, switching to infrastructure-as-code and immutable infrastructure and constructing software delivery pipelines. Another Andrey's passion is teaching software automation practices and tooling. His DevOps MasterClass (eXtreme Automation) course has been delivered more than 20 times in various locations in Europe: Austria, Sweden, Denmark, UK, Romania, Estonia, Latvia. Andrey is also a co-founder of DevChampions training center. Andrey is a frequent speaker at international conferences and local communities. He is one of the leaders of LatCraft - Latvian Software Craftsmanship Community as well as co-founder and organizer of DevTernity conference.

Updated on October 10, 2020

Comments

  • Andrey Adamovich
    Andrey Adamovich over 3 years

    I have the following ApplicationListener:

    package org.mycompany.listeners;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextStartedEvent;
    
    public class MyApplicationListener implements ApplicationListener<ContextStartedEvent> {
    
      public MyApplicationListener() {
        super();
        System.out.println("Application context listener is created!");
      }
    
      /**
       * {@inheritDoc}
       */
      public void onApplicationEvent(final ContextStartedEvent event) {
        System.out.println("Context '" + event.getApplicationContext().getDisplayName() + "' is started!");
      }
    
    }
    

    And the following bean definition:

    <bean name="myApplicationListener" class="org.mycompany.listeners.MyApplicationListener" />
    

    I can see that bean is created as message from the constructor is printed, but context start event is never recieved. What am I missing?

    • borjab
      borjab about 10 years
      Should It have a @Component annotation?
  • Andrey Adamovich
    Andrey Adamovich about 13 years
    I have no lazy-initalized beans in my context.
  • Piotr Findeisen
    Piotr Findeisen over 12 years
    Note that ContextRefreshedEvent may be published more than once, and therefore it may be also published before all the beans are initialized (e.g. when using CXF 2.4.2...). However, in most common setup, ContextRefreshedEvent will be indeed published only when contexted startup is finished.
  • Alina Danila
    Alina Danila over 10 years
    I think the afterPropertiesSet method applies to a single bean, not to the whole spring application context. docs.spring.io/spring/docs/2.5.6/api/org/springframework/bea‌​ns/…
  • Sagar balai
    Sagar balai almost 6 years
    very helpful but this conventions looks inappropriate :(