4.3 Lifecycle Events

New Feature in Spring 2.5

This has already been covered in more detail in the previous chapter, but Spring can use two annotations from JSR-250 (Commons Annotations) to indicate lifecycle events. The annotations @PostConstruct can be used to mark a method for callback as an initialization lifecycle event and the annotation @PreDestroy can be used for the destruction lifecycle event.

If more than just lifecycle annotations are used to define lifecycle events, there is a defined order they will run in. The annotated methods will be run before lifecycle interfaces, which run before the XML configured lifecycle methods. If the same method is accidentally defined more than once between these multiple configuration options, it will still only be run just once.

Table 4.6. Annotations from javax.annotation

AnnotationSince VersionTargetDescription
@PostConstructSpring 2.5Method If the CommonAnnotationBeanPostProcessor is installed as the bean is processed, it is called as part of the initialization lifecycle event. Part of JSR-250 (Commons Annotations).
@PreDestroySpring 2.5Method If the CommonAnnotationBeanPostProcessor is installed as the bean is processed, it is called as part of the destruction lifecycle event. Part of JSR-250 (Commons Annotations).