Java Spring boot Service bean创建顺序 [英] Java Spring boot Service bean creation order

查看:56
本文介绍了Java Spring boot Service bean创建顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring 启动应用程序中,我使用了几个带有 @Service 注释的 bean.我希望在创建所有其他服务 bean 之后创建其中一个 bean.我该怎么做?

In Spring boot application I use several beans with annotated with @Service. I want that one of this beans will be created after the all other service beans was created. How can I do it?

推荐答案

默认情况下,Spring 使用服务的依赖项来确定它创建服务的顺序.

By default, Spring uses the dependencies of your services to determine the order in which it creates your services.

如果您想要更好地控制服务的创建时间(即在所有其他服务之后创建一个服务,即使它没有所有依赖项),那么您可以使用 SmartLifeCycle.

If you want finer control about when your services are created (i.e. create one service after all others, even though it doesn't have all the dependencies), then you can use SmartLifeCycle.

这个类允许你定义不同的阶段来初始化(以及销毁)你的 bean.另见 Spring 框架文档中的启动和关闭回调.

This class allows you to define different phases for initializing (and also destroying) your beans. See also Startup and shutdown callbacks in the Spring Framework documentation.

这篇关于Java Spring boot Service bean创建顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆