简体   繁体   中英

Spring autowire only if bean is present as method argument

I am using @ConditionalOnProperty to create a 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. 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?

-- 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

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 = . Because if you don't have a property bean will created and if you will have a property with any value bean will created.

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 :

    @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);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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