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

Spring RabbitMQ无法解析占位符'queue.name'问题求助

解决@RabbitListener无法解析${queue.name}占位符的问题

问题重现

使用Spring RabbitMQ时,@RabbitListener注解中通过${queue.name}读取配置文件的队列名称,触发如下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'queue.name' in value "${queue.name}"

相关代码

Listener实现代码:

@RabbitListener(queues = {"${queue.name}"}) 
public void receive(@Payload String payload) {
    try {
        Alert temp = new ObjectMapper().readValue(payload, Alert.class);
        if(temp != null) {
            int idTemp = temp.getId();
            while(verifyID(idTemp) != -1) {
                idTemp++;
            }
            temp.setId(idTemp);
            alertArray.add(temp);
        }
    } catch(IOException e) {
        
    }
}

application.properties配置内容:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
queue.name=test

解决步骤

  • 检查配置文件路径:确保application.properties放在src/main/resources目录下,这是Spring Boot默认加载配置的路径。若配置文件在自定义目录,需通过@PropertySource指定路径,示例:

    @Configuration
    @PropertySource("classpath:config/application.properties")
    public class RabbitConfig {}
    
  • 确认环境配置激活状态:如果使用多环境配置(如application-dev.properties),需保证当前激活的环境配置文件中包含queue.name=test,或在主配置文件中通过spring.profiles.active指定正确的运行环境。

  • 验证组件扫描范围:启动类的@SpringBootApplication默认扫描自身所在包及子包,若Listener类不在该范围内,需通过@ComponentScan显式指定扫描路径,确保Spring能识别到该Listener组件。

  • 清理缓存并重建项目:执行mvn clean install(Maven项目)或./gradlew clean build(Gradle项目),清除IDE缓存后重启应用,避免配置文件未被正确编译到输出目录。

  • 临时排查测试:将@RabbitListener(queues = {"${queue.name}"})替换为@RabbitListener(queues = {"test"}),若应用能正常启动,说明问题出在配置读取环节;若仍报错,需排查RabbitMQ连接状态或目标队列是否存在。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:15:21