karthick
karthick

Reputation: 25

Spring Boot JMS integration

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)?

Upvotes: 1

Views: 245

Answers (1)

Swapnil Khante
Swapnil Khante

Reputation: 659

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.

Upvotes: 1

Related Questions