小记-- Attribute value must be constant

最近项目中使用spring-boot-starter-amqp来操作RabbitMQ,利用@RabbitListener来监听作为消费者:

@RabbitListener(queues = "queueName")

为方便修改需要将队列名放于配置文件中,一开始想到通过@Value从配置文件中取值,但是提示错误:

Attribute value must be constant

如图: 后面发现可以直接取值

@RabbitListener(queues = "${queue.name}")

@Value其实就是

@Value(value = "${queue.name}")

可以通过如下方式设置默认值,默认值为:abcdQueue

@Value(value = "${queue.name: abcdQueue}")

其他:()

@Value("normal")
    private String normal; // 注入普通字符串

    @Value("#{systemProperties[os.name]}")
    private String systemPropertiesName; // 注入操作系统属性

    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber; //注入表达式结果

    @Value("#{beanInject.another}")
    private String fromAnotherBean; // 注入其他Bean属性:注入beanInject对象的属性another
    
    @Value("classpath:com/hry/spring/configinject/config.txt")
    private Resource resourceFile; // 注入文件资源

    @Value("http://www.baidu.com")
    private Resource testUrl; // 注入URL资源

另: 关于SpEl表达式的一些知识:

经验分享 程序员 微信小程序 职场和发展