12

I have spring application(I haven't lazy beans).

I want to insert logic to place when all @Component(@Repositoey @Service @Controller) beans are initialized.

How can I make it?

  • Can't you use @ PostConstruct annotation on method e.g. @ PostConstruct private void init(){ ? – StanislavL Oct 23 '13 at 12:28
  • I need invoke method after **ALL BEANS** –  Oct 23 '13 at 12:31
  • 1
    Take a look at my answer, this solution lets you execute any code after each bean is initialized. – Michał Rybak Oct 23 '13 at 12:35
  • Possible duplicate of [Calling A Method After all SpringBeans and ApplicationContext have been initialized](http://stackoverflow.com/questions/3409605/calling-a-method-after-all-springbeans-and-applicationcontext-have-been-initiali) – OrangeDog Feb 02 '16 at 10:24

1 Answers1

17

As mentioned in the answer to this question, you could use ApplicationListener and look for the ContextRefreshedEvent:

public class Loader implements ApplicationListener<ContextRefreshedEvent>{

        public void onApplicationEvent(ContextRefreshedEvent event) {
                 // whatever you want to do when app context is initialized or refreshed
        }
}
Community
  • 1
  • 1
trf
  • 1,279
  • 1
  • 13
  • 33