dynamically set @JmsListener destination from configuration properties

You can't reference a field within the current bean, but you can reference another bean in the application context using a SpEL expression...

@SpringBootApplication
public class So49368515Application {

    public static void main(String[] args) {
        SpringApplication.run(So49368515Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(JmsTemplate template, Foo foo) {
        return args -> template.convertAndSend(foo.getDestination(), "test");
    }

    @JmsListener(destination = "#{@foo.destination}")
    public void listen(Message in) {
        System.out.println(in);
    }

    @Bean
    public Foo foo() {
        return new Foo();
    }

    public class Foo {

        public String getDestination() {
            return "foo";
        }
    }

}

You can also use property placeholders ${...}.


Using property placeholder is much easier.

@JmsListener(destination = "${mq.queue}")
public void onMessage(Message data) {

}