A Cleanup Bean in Spring

preview_player
Показать описание
Learn how to wire in a simple bean to clean up resources in a Spring application.

A simple Spring bean to help shutting down and cleaning up any resource that needs custom cleanup when the application shuts down.
Рекомендации по теме
Комментарии
Автор

Isn't it easier to do it in the CoreConfig class? You have all the beans at your disposal already. Have you created the Cleanup bean to separate the concerns? Also, how to release all the resources? Some of the beans are also not singletons(prototypes are rare but still exist). Looking forward to read your thoughts.

SilviuBurceaDev
Автор

is it required to clean up bean if we created using @Bean annotation ?

salmankhandu
Автор

Nice explanation. But why using eclipse (instead of IntelliJ)? Why do you define a logger instead of using @Slf4j?

fuxtnegrx
Автор

I see here two major drawbacks in this solution.

First, the executor pool will be instantiated, even if the application makes no use of it because it is autowired into the cleanup component.

While this is rather a minor issue as the application would probably use it anyway, the second more concerning thing is, that creating a bean in one class and destroying it in another is a big code smell to me.

So instead of defining one cleanup bean that handles all the shutdown processes of any resource in your application, you would do better if you had refactored the first class and put the creation and the destruction of the bean into a single component (like the @PreDestroy annotation suggests).

Greetings
Christian

ChristianSmolka
Автор

It seems like neither DisposableBean nor @PreDestroy work in case of Spring Boot: 'destroy' method never gets called

sva