Even Gary's answer is very good it is not working always. For example in Spring Boot with autoconfiguration and using RabbitListener
you probably end with some weird situation where application refuse to start or your factory will not be used. I would recommend to used BeanPostProcessor
instead. There is a simple example following:
@Component
public class ConsumerTagByAppSetter implements BeanPostProcessor {
private final String appName;
public ConsumerTagByAppSetter(@Value("${spring.application.name}") String appName) {
this.appName = appName;
}
@Override
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
if (bean instanceof SimpleRabbitListenerContainerFactory) {
((SimpleRabbitListenerContainerFactory) bean).setConsumerTagStrategy(q -> appName + "." + q);
}
return bean;
}
}
This post processor is applied to the factory created and customized by auto configuration and changes just the way how consumer tag is generated which I consider much safer and out of the box than trying to start from scratch...