将@ConfigurationProperties绑定到用于创建bean的构建器

时间:2018-08-23 10:26:02

标签: spring spring-boot caching caffeine

我正在创建多个咖啡因缓存,例如:

@Bean
public Cache<String, Customer> customerCache() {
    return Caffeine.newBuilder()
            .maximumSize(10_000)
            // other config settings
            .build(..);
}

现在,我想使用@ConfigurationProperties(prefix = "cache.customer")之类的东西来设置构建器配置选项。

存在应用程序属性cache.customer.maximum-size: 1000的地方。

可以将@ConfigurationProperties映射到Caffeine构建器吗?

3 个答案:

答案 0 :(得分:1)

对于以后的读者来说,这是我用来使用Spring Boot的@ConfigurationProperties来配置Caffeine缓存的代码:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
 * Base class for configuration of a Caffeine {@link Cache}
 */
public class CaffeineCacheProperties {

    private Integer maximumSize;

    // TODO: Add additional properties + getters and setters.

    public Integer getMaximumSize() {
        return maximumSize;
    }

    public void setMaximumSize(final Integer maximumSize) {
        this.maximumSize = maximumSize;
    }

    public Caffeine initializeCacheBuilder() {
        Caffeine cacheBuilder = Caffeine.newBuilder();
        if (maximumSize != null) {
            cacheBuilder.maximumSize(maximumSize);
        }
        // TODO: Configure additional properties.
        return cacheBuilder;
    }
}

import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * The cache {@link Configuration} class.
 */
@Configuration
public class CacheConfig {

    @Bean
    @ConfigurationProperties("cache.customer")
    public CaffeineCacheProperties customerCacheProperties() {
        return new CacheProperties();
    }

    @Bean
    public Cache<String, Customer> customerCache() {
        return customerCacheProperties().initializeCacheBuilder().build();
    }

    // TODO: Add other caches.
}

然后添加一个应用程序属性,例如:

cache.customer.maximum-size: 1000

答案 1 :(得分:0)

您可以执行类似于启动团队使用DataSourceProperties进行的操作:

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java

将属性绑定到属性类中,然后在该属性类上使用方法来创建构建器。

这是另一个示例,其中我们将属性和数据源都绑定到同一根:

    @Bean
    @ConfigurationProperties("datasource.task")
    public DataSourceProperties taskDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = {"taskDataSource"}, destroyMethod="")
    @ConfigurationProperties("datasource.task")
    @ConditionalOnMissingBean(name="taskDataSource")
    public DataSource taskDataSource() {
        return taskDataSourceProperties().initializeDataSourceBuilder().build();
    }

答案 2 :(得分:-1)

您可以在@ConfigurationProperties(prefix = "cache.customer")类(配置类)的顶部使用CacheConfig,在这里您可以使用@EnableConfigurationProperties(CacheConfig.class)轻松地将应用程序属性绑定到Cache类。 因此,现在您可以将CacheConfig类自动连接到Cache类,并将其另存为您的缓存类中的私有属性。然后您可以通过类似的构建器来使用该配置

@Bean
public Cache<String, Customer> customerCache() {
    return Caffeine.newBuilder()
            .maximumSize(cacheConfig.getMaximumSize())
            // other config settings
            .build(..);
}
相关问题