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 (ie in a separate class)?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.