Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring pass value from property file to annotation

Tags:

java

spring

I have application.properties file for spring app which contains some simple properties:

queue=my.test.q

in the java code i need to specify the queue to @RabbitListener:

@Component
public class Handler {    
    @RabbitListener(queues = "my.test.q")
    public void handleMessage(Message message) {
       ...
    }

that would work, but i want to pass parameter to the annotation, i've tried the following but none of them worked.

    @Component
    public class Handler {
        @Value("${queue}")
        private String queueName;

        @RabbitListener(queues = @Value("${queue}")  <-- not working
        @RabbitListener(queues = queueName))     <--- not working
        public void handleMessage(Message message) {
           ...
        }    

is it possible to that?

like image 608
user468587 Avatar asked Aug 20 '15 19:08

user468587


1 Answers

As you can see in the javadoc for the @RabbitListener annotation, the queues attribute is a table of Strings, so you cannot assign an annotation to it. You also cannot assign variables to annotation attributes at all in Java, as they are required to be compile constants.

I cannot test it right now, but the javadoc seems to suggest that this should work (note, that it says it may return SpEL expressions):

@RabbitListener(queues = "${queue}")     
public void handleMessage(Message message) {
    ...
}    
like image 116
Apokralipsa Avatar answered Oct 05 '22 23:10

Apokralipsa