Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application lifecycle events with spring?

Tags:

java

spring

Establishing connections to external systems (databases etc) as part of the spring context construction seems like bad idea. Does the framework contain any method for actual application lifecycle management ? Since I'm running in embedded-mode, my best suggestion yet is to use a ListableBeanFactory to get all the beans and make an interface with start/stop/isRunning on all the application beans. Do I have to roll my own or is there something within spring I can use ?

like image 326
krosenvold Avatar asked Jun 12 '14 06:06

krosenvold


1 Answers

Spring has the Lifecycle interface which you can implement. This interface provides basic start, stop and isRunning methods.

You could also implement the SmartLifecycle interface which allows for a little more control over when something is invoked due the the additional Phased interface.

See also How does Lifecycle interface work in Spring? What are "top-level singleton beans"? and Load spring context in phases.

like image 71
M. Deinum Avatar answered Oct 27 '22 22:10

M. Deinum