Sobes.tech
Junior — Middle

How does defining the scope of a Bean affect the execution of a method annotated with PostConstruct?

sobes.tech AI

Answer from AI

The @PostConstruct annotation in Java is used to designate a method that should be called after the container has finished creating and injecting dependencies into the Bean.

The scope of the Bean affects the timing of the call to the method with @PostConstruct:

  • For singleton Beans, the method is called once after the instance is created at application startup.
  • For prototype Beans, the method is called each time a new instance is created.

Thus, the Bean's lifecycle determines when exactly and how many times the method with @PostConstruct will be called.

How does defining the scope of a Bean affect the… - sobes.tech