I am a little confused about the result of below code.
ParentController:
@Controller
public abstract class ParentController{
@PostConstruct
public void init(){
System.out.println("Parent-----PostConstruct");
}
public ParentController(){
System.out.println("Parent-----constructor");
}
}
ChildController:
@Controller
public class ChildController extends ParentController {
@PostConstruct
public void init() {
System.out.println("Child-----PostConstruct");
}
public ChildController(){
System.out.println("Child-----constructor");
}
}
the result is below:
Parent-----constructor
Child-----constructor
Child-----PostConstruct
Parent-----PostConstruct
I don't know why parent's postConstruct is after child's postContruct.
This happens because you are overriding @PostConstruct method.
What is going on:
Constructor of child class is called.
Constructor of child class calls super
Constructor of parent class is called
Constructor of parent class is executed
Constructor of parent class is finished
Constructor of child class is executed
Constructor of child class is finished
@PostConstruct of child class is called, executed, and finished(because we called the constructor of child class)
@PostConstruct of parent class is called, executed, and finished(because we called the constructor of parent class)
UPD: (thanks @Andreas for this!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With