spring 如何在Sping Boot 中使用@ConditionalOnBean和@限定符?

bkkx9g8r  于 2022-12-10  发布在  Spring
关注(0)|答案(2)|浏览(170)

目前,我有以下两门课:

@Configuration
@Import(AssumeRoleSQSConfig.class)
public class AssumeRoleConfig {
    public static final DynamicStringProperty ROLE_ARN = DynamicPropertyFactory.getInstance()
            .getStringProperty("role.arn", null);
    public static final DynamicStringProperty ROLE_SESSION = DynamicPropertyFactory.getInstance()
            .getStringProperty("role.session.name", null);
    
    @Bean(name = "assumeRole")
    public AWSCredentialsProvider getCredentials() {
        if (ROLE_ARN.get() == null || ROLE_SESSION.get() == null) {
            return null;
        }
        ... omitted ...
    }
}

而且,我在config.properties中设置了这两个属性。

@Configuration
public class AssumeRoleSQSConfig {
    @Bean(name = "sqsAssumeBean")
    @ConditionalOnBean(name = "assumeRole")
    public AmazonSQSAsync sqsAssume(
        @Qualifier("assumeRole") AWSCredentialsProvider awsCredentials) {
        ... omitted ...
    }
}

我还有这两个默认bean:
第一次
如果另一个类尝试访问“sqsAssumeBean”和“defaultSQS”,如下所示:

@Configuration
@Import({DefaultCredentialsConfig.class, AssumeRoleConfig.class})
public class QueueConfig {
    
    private AmazonSQSAsync defaultSQS;
    private AmazonSQSAsync assumeRoleSQS;

    @Autowired
    public QueueConfig (
        @Qualifier("defaultSQS") AmazonSQSAsync defaultSQS,
        @Qualifier("sqsAssumeBean") AmazonSQSAsync assumeRoleSQS) {
        this.defaultSQS = defaultSQS;
        this.assumeRoleSQS = assumeRoleSQS;
    }
}

然后,我得到以下错误:

Error creating bean with name 'queueConfig' defined in ... QueueConfig.class 
Unsatisfied dependency expressed through constructor parameter 1; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.amazonaws.services.sqs.AmazonSQSAsync' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="sqsAssumeBean")}

Description:

Parameter 1 of constructor in QueueConfig required a bean of type 'com.amazonaws.services.sqs.AmazonSQSAsync' that could not be found.

The following candidates were found but could not be injected:
    - User-defined bean method 'defaultSQS' in 'DefaultSQSConfig'

据我目前所知,QueueConfig构造函数查找bean“sqsAssumeBean”,但它无法找到“assumeRole”bean,因为它尚未创建。因此,QueueConfig构造函数查找“defaultSQS”bean而不是“sqsAssumeBean”,但它无法被注入,因为@Qualifier专门查找“assumeRole”bean。
我调试了应用程序,发现AssumeRoleConfig“getCredentials”方法甚至没有被调用。这意味着AssumeRoleSQSConfig甚至没有尝试获取bean“assumeRole”,但如果我删除@ConditionalOnBean,它确实会尝试获取。为什么@ConditionalOnBean会采取主动,并发现没有创建“assumeRole”bean,而是让“assumeRole”bean被创建,如果它为空,然后主动声明没有“assumeRole”bean?谢谢!
(Note:我有ROLE_ARN和ROLE_SESSION动态属性。因此,我尝试做的是,如果ROLE_ARN/ROLE_SESSION动态属性未设置为/null,则不应创建“assumeRole”bean。这也应该有条件地不创建“sqsAssumeBean”。这也应该有条件地不创建“queueConfig”bean。但是,目前,即使我设置了这两个动态属性,根本不会调用创建“assumeRole”bean的方法。)

hts6caw3

hts6caw31#

您遇到的问题是注入可选依赖项。
有三种最常用的方法来实现它:
1.在参数级别使用org.springframework.lang.Nullable注解
1.使用java.util.Optional使参数成为可选参数
1.定义多个@Autowired(required = false)构造函数,只定义必需的参数
Spring@自动布线参考

mrwjdhj3

mrwjdhj32#

不确定是否有帮助,但你试过@AutoConfigureAfter@AutoConfigureBefore

相关问题