Search code examples
javaspringcomponents

Spring @Component creation order


I tried to find answer on the question above, but I didn't. So, What is Spring @Component creation order? For example: We have

@Component
public class Foo extends SecondClass {
   private SomeType someField;

   @Autowired
   public Foo(SomeType someField){
    super(someField);
   }
}

public class SecondClass implement ISomething {
  //code and @override methods...
  @PostConstruct 
  public void method(ISomething i) {
    //Actions with i
  }
}

What will be creation order? What will be created first, parent or child in this particular case? Thanks..


Solution

  • For simplicity sake, ignore the beans that will be created in the background as part of a Spring application.

    Since you have @Component on class Foo alone, single Spring bean will be created. It does not matter the creation order because there is only one bean being created here since you do not have @Component on any other class. You will have to manually instantiate SecondClass by yourself if you want to make use of it.

    Regarding inheritance question, Spring does not get involved with that. It will be taken care of by Java itself.

    edit:

    @PostConstruct will be ignored because SecondClass is not a Spring bean. But since we are using super, it will be called This is a complete program that tests creation order of bean.

    import javax.annotation.PostConstruct;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.stereotype.Component;
    
    @SpringBootApplication
    public class TestProgram implements CommandLineRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(TestProgram.class, args);
        }
    
        @Component
        public static class Foo extends SecondClass {
            @Override
            public void method() {
                System.out.println("Printing Foo class");
                //new change
                super.method(); 
            }
        }
    
        public static class SecondClass implements Cloneable {
            //Since you are calling this method via super in Foo class, you don't need 
            //this annotation as it is being ignored anyway since this class is not a 
            //bean.
            @PostConstruct
            public void method() {
              System.out.println("Printing Second Class");
            }
        }
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Spring application is up.");
        }
    }
    

    It will print both now as we are using super call to invoke SecondClass method.

    Printing Foo class

    Printing Second Class

    You don't need @PostConstruct in SecondClass as it is not a Spring bean and that's why it was getting ignored without super call.

    Play around by removing/adding annotations and you will get it.