Spring Boot 自动配置顺序 [英] Spring Boot AutoConfiguration Order

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

问题描述

我在使用 Spring Boot 在多模块 Maven 项目中以所需的顺序应用一些 Spring 配置时遇到了一些麻烦.

I am having some trouble getting some Spring configuration to be applied in the desired order with Spring Boot in a multi-module Maven project.

我有模块 A 和 B 由我编写,并且依赖于我在模块 C 中无法控制的第三方模块(依赖关系如下:A 依赖于 C,B 依赖于 A)

I have modules A and B that are written by me and a dependency on a third party module that I have no control over in module C (dependencies are as follows: A depends on C, B depends on A)

在模块 A 中,我有一个用 @Configuration@AutoConfigureBefore(ClassFromModuleD.class) 注释的类.在模块 B 中,我有另一个用 @Configuration@AutoConfigureBefore(ClassFromModuleA.class)

In module A I have a class annotated with @Configuration and also @AutoConfigureBefore(ClassFromModuleD.class). In module B I have another class annotated with @Configuration and also @AutoConfigureBefore(ClassFromModuleA.class)

我希望这会导致我的模块 B 中的 bean 定义首先被配置,然后是我的模块 A 配置类中的 bean,最后是 C 中的那些.

I was hoping that this would result in the bean definitions in my module B being configured first, followed by beans in my module A configuration class then finally the ones in C.

我还尝试向模块 A 和 B 中添加一个 META-INF/spring.factories 文件,该文件声明了存在于其自己模块中的单个配置文件.例如对于模块 A

I also tried adding a META-INF/spring.factories file to both module A and B which declares the single configuration file present in its own module. E.g. for module A

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.exmaple.moduleAConfiguration

在模块 B 中:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.exmaple.moduleBConfiguration

我没有看到想要的配置顺序,事实上,它似乎与我想要的完全相反.我使用了日志语句和调试器来逐步执行,似乎首先应用模块 C 中的配置,然后是 A,最后是 B.

I am not seeing the desired order of configuration, in fact, it seems to be the exact opposite to what I want. I have used logging statements and a debugger to step through and it seems the config from Module C is applied first, followed by A then finally B.

谁能指出我可能遗漏了什么,或者是否有其他方法可以做到这一点?非常感谢.

Could anyone point out what I may have missed or if there is another way to do this? thanks very much in advance.

推荐答案

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

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

这用于例如如果 Hibernate 在类路径上,则提供基本的 Jpa 配置.

This is used e.g. to provide a basic Jpa configuration if Hibernate is on the classpath.

如果要配置spring实例化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".

This would create bean "A", than "B".

但是,您想要的顺序可能无法实现.你写道:

However, the order you desire may not be possible. You wrote :

A依赖于C,B依赖于A

A depends on C, B depends on A

如果 'depends on' 意味着:A 需要实例化 C,bean 必须按以下顺序创建:

If 'depends on' means : A needs C to be instantiated, the beans must be created in the following order :

  1. C - 因为 C 不依赖任何东西
  2. A - 因为 A 依赖于已经创建的 C.
  3. B - 因为 B 依赖于已经创建的 A.

Spring 通过分析 bean 类自动检测依赖项.

Spring automatically detects the dependencies by analyzing the bean classes.

如果 A 具有自动装配属性或类型为 C 的构造函数参数,则 spring '知道'它必须在 A 之前实例化 C.

If A has an autowired property or a constructor argument of type C, spring 'knows' that it must instantiate C before A.

在大多数情况下,这非常有效.

In most cases this works quite well.

在某些情况下,spring 无法猜测"依赖项并以不需要的顺序创建 bean.比您可以通过 @DependsOn 注释通知" spring 依赖项.Spring 将尝试相应地更改顺序.

In some cases spring cannot 'guess' the dependencies and creates beans in an unwanted order. Than you can 'inform' spring through the @DependsOn annotation about the dependency. Spring will try to change the order accordingly.

在您的情况下,如果您描述的依赖项对 spring 不可见,并且不需要依赖项来创建 bean,您可以尝试使用 @DependsOn 更改顺序:

In your case, if the dependencies you described are not visible for spring, and the dependencies are not needed to create the beans, you can try to change the order with @DependsOn :

A依赖于C,B依赖于A

A depends on C, B depends on A

可以通过

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

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

// C comes from another module
// and no need to annotate

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

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