Mefisto_Fell
Mefisto_Fell

Reputation: 916

Questions about @PostConstruct

I have a couple questions about @PostConstruct in Spring.

  1. @PostConstruct is thread safe?
  2. Does this open a new thread for the init method or how it works?

I will be grateful for the answers)

Upvotes: 1

Views: 803

Answers (2)

Arun Prasat
Arun Prasat

Reputation: 360

@PostConstruct is thread safe. It only runs once after the bean created in the whole lifecycle of the bean.

Upvotes: 0

Mark Bramnik
Mark Bramnik

Reputation: 42441

  1. Yes, Spring calls @PostConstruct marked methods the object before it gets available to the application (Before injection). This is a part of bean creation.

  2. No. Spring creates an internal structure called "bean definition" during the application context initialization. Its a meta data about the bean. If the bean has "post-construct" method spring knows this and calls the relevant method during the bean creation.

Upvotes: 2

Related Questions