1
votes

I am going through the Spring Boot JMS guide. Here the JMSTemplate is initialized in the main method using context.getBean. How can I initialize JMSTemplate outside the main method (i.e. in a separate class)?

1

1 Answers

1
votes

You can have a separate config class for creating your jms configuration as follows :

@Configuration
public class JmsConfig {

@Bean
public MessageConverter messageConverter() {
  MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
  converter.setTargetType(MessageType.TEXT);
  converter.setTypeIdPropertyName("_type");
  return converter;
 }
}

Once you are done with configuration you can fetch the JMSTemplate bean from any class, for example ;

@Component
public class HelloSender {

  private final JmsTemplate jmsTemplate;

   public HelloSender(JmsTemplate jmsTemplate) {
   this.jmsTemplate = jmsTemplate;
  }
}

Here your JMSTemplate bean is getting autowired using constructor injection.