SpringBoot中的配置顺序 [英] Order of Configuration in SpringBoot

查看:115
本文介绍了SpringBoot中的配置顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解我们使用 @Configuration 制作的bean如何倾向于覆盖SpringBoot默认生成的bean。我一直致力于一个项目,在很多情况下我们为像ZuulConfigs这样的东西创建bean,并且假设是,无论我们做什么都应该优先于默认生成的bean。我一直试图解决这个问题,但不能。基本上,

I am trying to understand how beans that we make using @Configuration tends to override the beans that are generated by SpringBoot by default. I have been working on a project where in many cases we create beans for things like ZuulConfigs and the assumption is, whatever we are making shall take precedence over the default generated bean. I have been trying to figure this out but can't. Basically,


  1. Spring是通过一些自定义类加载器实现的吗

  2. 如果不是这样的优先级如何工作。我能否以类似的方式给我的bean提供一些优先权

  3. 我可以在项目中生成类似的层次结构,如果是这样的话

非常感谢帮助

推荐答案

Spring AutoConfiguration用于提供基本配置,如果某些类是是否在类路径中。

Spring AutoConfiguration is used to provide a basic configuration if certain classes are in the classpath or not.

如果要配置弹簧实例化bean的顺序,可以使用

If you want to configure the order in which beans are instantiated by spring you can use

@DependsOn("A") 
 public class B {
 ...    
}

这会创建beanA,然后是B。因此,您可以根据首先需要的bean来订购配置。无论如何Spring通过分析bean类自动检测依赖关系。
获得更多帮助,请查看此问题
Spring Boot AutoConfiguration Order

This would create bean "A", then "B". Hence you can order the configuration depending upon the beans need first to be done. Anyways Spring automatically detects the dependencies by analyzing the bean classes. for more help check this question Spring Boot AutoConfiguration Order

替代方案:
还有@AutoConfigureOrder注释(您可以优先配置),您可以查看代码以便更深入地理解。

Alternative : There is also "@AutoConfigureOrder" annotation(where you can prioritise the configuration), you can have a look in the code for deeper understanding.

AutoConfiguration的文档是这里

Documentation of AutoConfiguration is here

这篇关于SpringBoot中的配置顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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