简体   繁体   English

仅当bean作为方法参数存在时,Spring才会自动装配

[英]Spring autowire only if bean is present as method argument

I am using @ConditionalOnProperty to create a FileCompressor bean: 我正在使用@ConditionalOnProperty来创建FileCompressor bean:

@Bean
@ConditionalOnProperty(prefix = "file.rollover.sink", name = "compress", matchIfMissing = true)
public FileCompressor fileCompressor() {
    return new DefaultFileCompressor(...);
}

I would like to autowire FileCompressor bean only if it is present, null if file.rollover.sink.compress=false as a method argument. 我想仅在FileCompressor bean存在时自动装配,如果file.rollover.sink.compress=false作为方法参数,则file.rollover.sink.compress=false null But if I try to define it like: 但是,如果我尝试将其定义为:

@Bean
public RolloverTask rolloverTask(final IntervalCalculator intervalCalculator, final @Autowired(required = false) FileCompressor fileCompressor) {
    return new RolloverTask(intervalCalculator, fileCompressor);
}

I am getting the following error: 我收到以下错误:

Parameter 1 of method rolloverTask in com.example.FileRolloverSinkConfiguration required a bean of type 'com.example.compressor.FileCompressor' that could not be found.
    - Bean method 'fileCompressor' in 'FileRolloverSinkConfiguration' not loaded because @ConditionalOnProperty (file.rollover.sink.compress) found different value in property 'compress'

What changes should I make to autowire or pass null if not present? 如果不存在,我应该对autowire进行哪些更改或传递null

-- EDIT -- - 编辑 -

My solution: 我的解决方案

private FileCompressor fileCompressor;

@Autowired(required = false)
public void setFileCompressor(final FileCompressor fileCompressor) {
    this.fileCompressor = fileCompressor;
}


@Bean
public RolloverTask rolloverTask(final IntervalCalculator intervalCalculator) {
        log.info("Creating a new rollover task with{} a file compressor", fileCompressor == null ? "out" : "");
        return new RolloverTask(intervalCalculator, fileCompressor);
}

@Bean
@ConditionalOnProperty(prefix = "file.rollover.sink", name = "compress", matchIfMissing = true)
public FileCompressor fileCompressor() {
    return new DefaultFileCompressor(...);
}

I think you can use the annotations @ConditionalOnBean and @ConditionalOnMissingBean 我认为你可以使用注释@ConditionalOnBean@ConditionalOnMissingBean

I didn't try the code but it should be like this : 我没有尝试代码但它应该是这样的:

@Bean
@ConditionalOnBean(FileCompressor.class)
public RolloverTask rolloverTask(final IntervalCalculator intervalCalculator, final FileCompressor fileCompressor) {
    return new RolloverTask(intervalCalculator, fileCompressor);
}

and

@Bean
@ConditionalOnMissingBean(FileCompressor.class)
public RolloverTask rolloverTask(final IntervalCalculator intervalCalculator) {
    return new RolloverTask(intervalCalculator, null);
}

It doesn't make a sense to have matchIfMissing = true without havingValue = . 没有havingValue = matchIfMissing = true没有havingValue = Because if you don't have a property bean will created and if you will have a property with any value bean will created. 因为如果你没有创建属性bean,并且你将拥有一个具有任何值的属性bean将被创建。

You can solve it in this way: 你可以用这种方式解决它:

    @Autowired(required = false)
    private FileCompressor fileCompressor;

    @Bean
    public RolloverTask rolloverTaskWithCompressor(final IntervalCalculator intervalCalculator, final FileCompressor fileCompressor) {
        return new RolloverTask(intervalCalculator, fileCompressor);
   }

or have different bean definitions for both versions of RolloverTask : 或者对于两个版本的RolloverTask都有不同的bean定义:

    @Bean
    @ConditionalOnProperty(prefix = "file.rollover.sink", name = "compress", havingValue = "no", matchIfMissing = false)
    public RolloverTask rolloverTask(IntervalCalculator intervalCalculator) {
        return new RolloverTask(intervalCalculator, null);
    }

    @Bean
    @ConditionalOnProperty(prefix = "file.rollover.sink", name = "compress", havingValue = "yes", matchIfMissing = true)
    public RolloverTask rolloverTaskWithCompressor(final IntervalCalculator intervalCalculator, final FileCompressor fileCompressor) {
        return new RolloverTask(intervalCalculator, fileCompressor);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM