spring boot动态获取@Document中的indexName

本文介绍了在Spring Boot应用中整合Kafka和Elasticsearch存储日志时,如何动态获取@Document注解中的indexName。原本设置为固定值导致无法动态更改,通过引入Spring Cloud Config Client,使用配置中心解决@Document注解中indexName的动态配置问题,解决了运行时报错的困境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}错误解决

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值