title: spring-amqp Multiple queues with different routingKey
body: <p>I have recently started learning Spring and spring-amqp so this question might appear very basic so please excuse me for that.</p>
  
  <p>I have multiple queues which are on different hosts and have different QueueName, RoutingKey, vhost, user, password. I am writing the publishing logic for these queues and was not able to decide if I should have one configuration class per queue or can it be done in XML. </p>
  
  <p>The method of creating a class to have all the information about the queue (host, vhost, username etc) is working fine as described in <a href="https://github.com/spring-projects/spring-amqp-samples/blob/master/helloworld/src/main/java/org/springframework/amqp/helloworld/HelloWorldConfiguration.java" rel="nofollow">this example</a>.
  I created a @Configuration class and defined all the beans for that Queue. But then I need to do</p>
  
  <pre><code>ApplicationContext context = new AnnotationConfigApplicationContext(MyQueueConfiguration.class);
  AmqpTemplate amqpTemplate  = context.getBean(AmqpTemplate.class);
  amqpTemplate.convertAndSend("Hello World!!");
  </code></pre>
  
  <p>So my requirement is:</p>
  
  <ol>
  <li>Since I have many queues that need to be instantiated at the application startup, once tomcat starts the connections/channels to the queue/rabbit cluster should be established.</li>
  <li>Then as soon as there is POST request that comes to my application I need to publish the message to one of the queue based on a POST parameter.</li>
  </ol>
  
  <p>So for each queue do I always need to do: </p>
  
  <pre><code>ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
  </code></pre>
  
  <p>Or is there a way to Spring load the all my Queue configuration classes and just use the object like:</p>
  
  <pre><code>    // calling code when I get a POST request
  MyQueueConfigurationClass.publishMessage(payload, queueName);
  
  // The implementation code
  public boolean publishMessage(String payload, String queueName){
  
      // Get Bean for the **queueName** somehow
      AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
      // Use the bean to send the message
      amqpTemplate.convertAndSend(payload);
  
  }
  </code></pre>
  
  <ol>
  <li>So how can I get the amqpTemplate for the exact queue without doing <code>new AnnotationConfigApplicationContext()</code> everytime?</li>
  <li>What is the harm of doing new AnnotationConfigApplicationContext every time a request comes to my service? [I am guessing that creating a new object for each request is not a good idea]</li>
  </ol>