Spring:如果存在组件,则对其进行优先级排序



我有三个具有相同超类的组件,它们都以属性为条件。我只想自动连接其中一个组件,按特定顺序排列优先级。我想过使用@Order,但我想知道它是否可以在这种情况下使用,或者只是订购这些组件的集合?

组件

@Component
@ConditionalOnProperty("stable.enabled")
public class StableChild extends ParentClass {}
@Component
@ConditionalOnProperty("experimental.enabled")
public class ExperimentalChild extends ParentClass {}
@Component
@ConditionalOnProperty("testing.enabled")
public class TestingChild extends ParentClass {}

我打算如何使用

@Component
public class WhateverClass {
/**
* I want to autowire TestingChild if it is present, otherwise ExperimentalChild. If it
* is also not present, then StableChild. If all of them are absent it is okay to throw 
* the exception because something is wrong with the configurations.
*/
@Autowired
private ParentClass component;
}

我认为这里不需要@Order。您所需要做的就是使用Conditional Of Missing bean

最新更新