Skip to content
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:

  1. Invoke constructor (Autowired constructor, if available, otherwise the empty constructor).
  2. Perform Autowired field injections.
  3. Perform Autowired setter injections.
  4. Invoke PostConstruct method.

PostConstruct Example

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);
    }

    // ...

}
Clone this wiki locally