Why use @PostConstruct?

后端 未结 5 814
孤街浪徒
孤街浪徒 2020-11-22 11:49

In a managed bean, @PostConstruct is called after the regular Java object constructor.

Why would I use @PostConstruct to initialize by bean

5条回答
  •  轮回少年
    2020-11-22 12:33

    If your class performs all of its initialization in the constructor, then @PostConstruct is indeed redundant.

    However, if your class has its dependencies injected using setter methods, then the class's constructor cannot fully initialize the object, and sometimes some initialization needs to be performed after all the setter methods have been called, hence the use case of @PostConstruct.

提交回复
热议问题