简体   繁体   中英

Application lifecycle events with 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 ?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM