spring boot动态获取@Document中的indexName

spring boot动态获取@Document中的indexName

在做spring boot 整合kafka+es做日志存储时发生

本身是这样的

@Document(indexName = "xxx", type = "xxx", shards = 1,replicas = 0, refreshInterval = "-1")

但是这样是固定值,不可动态更改
后来改成这样

@Document(indexName = "#{@indexName}", type = "xxx", shards = 1,replicas = 0, refreshInterval = "-1")

当然不是只这样就行
要加这个

@Component
public class ConfigBean {
    @Value("${configBean.indexName}")
    private String indexName;
    @Bean
    public String indexName(){
        return indexName;
    }
}

解决办法取自动态的为ElasticSearch的@Document指定index
加了以后一直报错
Could not resolve placeholder 'configBean.indexName' in value "${configBean.indexName}"
最后找到解决办法
在入口类添加

	@Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {

        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();

        c.setIgnoreUnresolvablePlaceholders(true);

        return c;

    }

解决办法取自SpringCloud的Config Client端Could not resolve placeholder ‘xxx’ in value "${xxx}错误解决

你可能感兴趣的:(日常bug)