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

时间:2017-01-27 13:53:23

标签: java spring spring-boot

我使用@ConditionalOnProperty创建FileCompressor bean:

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

我想仅在FileCompressor bean存在时自动装配null如果file.rollover.sink.compress=false作为方法参数。 但是,如果我尝试将其定义为:

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

我收到以下错误:

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'

如果不存在,我应该对自动装配或通过null进行哪些更改?

- 编辑 -

我的解决方案:

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(...);
}

2 个答案:

答案 0 :(得分:2)

我认为您可以使用注释@ConditionalOnBean@ConditionalOnMissingBean

我没有尝试过代码,但它应该是这样的:

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

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

答案 1 :(得分:1)

matchIfMissing = true没有havingValue =是没有意义的。因为如果您没有创建属性bean,并且如果您将拥有一个具有任何值的属性,那么将创建bean。

你可以用这种方式解决它:

    @Autowired(required = false)
    private FileCompressor fileCompressor;

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

或对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);
   }