You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SlpStoreInterceptor类获取Spring容器中对应Bean的ID?

获取指定类型Spring Bean的ID

已知Spring上下文仅存在一个SlpStoreInterceptor类对应的Bean,可通过以下两种方式获取其ID:

  • 使用ApplicationContext的getBeanNamesForType方法:
    该方法会返回对应类型的所有Bean名称数组,由于上下文仅存在一个该类型的Bean,直接取数组第一个元素即可得到目标ID。

    代码示例:

    // 先获取Spring应用上下文实例(可通过Autowired注入等方式获取)
    ApplicationContext applicationContext = ...;
    // 获取对应类型的所有Bean名称
    String[] beanIds = applicationContext.getBeanNamesForType(SlpStoreInterceptor.class);
    // 取第一个元素即为目标Bean的ID(slpStore)
    String targetBeanId = beanIds[0];
    
  • 通过Bean实例获取ID:
    如果已经持有该Bean的实例,可直接调用ApplicationContext的getBeanName方法获取其ID:

    SlpStoreInterceptor beanInstance = applicationContext.getBean(SlpStoreInterceptor.class);
    String targetBeanId = applicationContext.getBeanName(beanInstance);
    

内容的提问来源于stack exchange,提问作者Joel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 23:48:22