-
Notifications
You must be signed in to change notification settings - Fork 1
PostConstruct
tylertreat edited this page Dec 31, 2012
·
1 revision
The PostConstruct annotation indicates that the annotated method is to be invoked after the bean has been instantiated by Infinitum. Only one method can carry this annotation, and it does not have to be public. A method marked with this annotation must not have any arguments.
A PostConstruct method can be used to do additional bean initialization which relies on Autowired dependencies. The reason this initialization cannot be done in the constructor is because autowiring does not occur until after the constructor has been invoked. The bean initialization process is the following:
- Invoke constructor (
Autowiredconstructor, if available, otherwise the empty constructor). - Perform
Autowiredfield injections. - Perform
Autowiredsetter injections. - Invoke
PostConstructmethod.
The below example illustrates the usage of the PostConstruct annotation.
@Bean
public class MyBean {
@Autowired
private FooBean mFooBean;
private int mValue;
public MyBean() {
mValue = 42;
}
@PostConstruct
private void init() {
mFooBean.doSomething(mValue);
}
// ...
}