如何在模块的 Spring Boot 配置类中使用 @AutoConfigureOrder [英] How to use @AutoConfigureOrder in a spring boot configuration class from a module

查看:41
本文介绍了如何在模块的 Spring Boot 配置类中使用 @AutoConfigureOrder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Spring Boot 项目中,我使用了一个模块,它提供了一个 @Configuration 类.配置是通过Application类中的@Import(ConfigurationFromModule.class)导入的.

In my spring boot project I use a module, which provides a @Configuration class. The configuration ist imported via @Import(ConfigurationFromModule.class) in Application class.

模块的配置需要在某个AutoConfiguration之前发生.不幸的是,模块类中似乎忽略了定义配置顺序的注释.

The configuration from the module needs to happen before a certain AutoConfiguration. Unfortunately annotations defining the order of configurations seem to be ignored in the modules classes.

我用 @AutoConfigureBefore({SomeBuildInAutoConfiguration.class}) 注释了配置,但仍然在 ConfigurationFromModule 之前触发了 SomeBuildInAutoConfiguration.

I annotated the Configuration with @AutoConfigureBefore({SomeBuildInAutoConfiguration.class}), but still SomeBuildInAutoConfiguration was triggered before ConfigurationFromModule.

出于好奇,我试图用 @AutoConfigureOrder(1)@Primary 注释 ConfigurationFromModule - 但它们没有改变任何东西.

Out of curiosity I tried to annoatate ConfigurationFromModule with @AutoConfigureOrder(1) and @Primary - but they don't change anything.

对于主要应用程序中的 @Configuration 类,所有这些注释都按预期工作.

For @Configurationclasses within the main applications all those annotations work as expected.

如何定义模块内自动配置的顺序?

How to define order of AutoConfiguration within a module?

推荐答案

Spring AutoConfiguration 提供了一个基本配置,如果某些类在类路径中与否.

Spring AutoConfiguration provides a basic configuration if certain classes are in the classpath or not.

如果你想要这样的配置,你可以使用 @DependsOn

If you want that configuration in order you can make use of @DependsOn

@DependsOn("One") 
public class Two{
    
}

这有助于您创建 beanOne",然后创建 beanTwo".

This helps you to create bean "One", then bean "Two".

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

Spring automatically detects the dependencies by analyzing the bean classes.

如果 Bean One 有一个自动装配的属性或一个类型为二的构造函数参数,spring 知道它必须在一个之前实例化两个.

If Bean One has an autowired property or a constructor argument of type Two, spring knows that it must instantiate Two before One.

这篇关于如何在模块的 Spring Boot 配置类中使用 @AutoConfigureOrder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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