0

I want to create some info logging to console when all spring content is initialized.

This includes all @Autowired dependencies resolved, all properties injected, and all @PostConstruct methods finished.

That's also why I can't use @PostConstruct here, as the order of multiple post annotations cnanot be guaranteed.

So, how could I achieve this?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    possible duplicate of [catch moment when spring initialized all beans](http://stackoverflow.com/questions/19541600/catch-moment-when-spring-initialized-all-beans) – M. Deinum Jan 15 '15 at 14:21

2 Answers2

1

Found the answer now:

public class Loader implements ApplicationListener<ContextRefreshedEvent> {
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
                 // TODO
        }
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120
0

If you explicitly create and start the Application context then you can run code when the context initialization is complete. The code would look like:

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:path/to/spring/context.xml");
LOGGER.info("Initializing Spring context...");
appContext.start();
LOGGER.info("Spring context initialization complete.");
Richard Neish
  • 8,414
  • 4
  • 39
  • 69