user3470960
user3470960

Reputation:

Is Spring's PostConstruct annotation tied to the session?

In an API, I have two classes A and B, which implement an interface C. I have a DAO (we can call D) which uses Spring's beanFactory to load either A or B, depending on a value in a property file. This property is tied to a database value.

I want to use Spring's PostConstruct annotation to write the following:

@PostConstruct    
public void setUp(){ 
    C file = (C) beanFactory.getBean(propertyValue);
}

But I'm not clear on whether Spring's PostConstruct will be called only one time when an application is compiled, or if it is executed once per session?

Additionally, I am using:

@Value("${property.variable.value}")
private String propertyValue;

Which is effectively pulling the value correctly from whatever property file was loaded in session.

Upvotes: 0

Views: 732

Answers (1)

user41871
user41871

Reputation:

It is tied to bean instantiation, which is independent of the bean's scope.

If the bean is a normal, application-scoped singleton, then Spring will call the method only once; namely, the one time that Spring creates the bean. If the bean is prototype-scoped, then Spring will call @PostConstruct every time it creates the bean.

Upvotes: 1

Related Questions