From 9c733128ac942ac4675b27d2d358afa941fea5fd Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 20 Jan 2016 18:18:38 +0100 Subject: [PATCH] Use ConditionalOnSingleCandidate when appropriate This commit updates various auto-configuration to use `@ConditionalOnSingleCandidate` rather than assuming that at most one bean of a given type will be available. Closes gh-2784 --- .../amqp/RabbitAutoConfiguration.java | 101 ++++++++++-------- .../jms/JmsAutoConfiguration.java | 50 +++++---- 2 files changed, 85 insertions(+), 66 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 88c0be73cc..ec1bfaab9d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -34,6 +34,7 @@ import org.springframework.boot.autoconfigure.amqp.RabbitProperties.Template; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -84,50 +85,6 @@ import org.springframework.retry.support.RetryTemplate; @Import(RabbitAnnotationDrivenConfiguration.class) public class RabbitAutoConfiguration { - @Bean - @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true) - @ConditionalOnMissingBean(AmqpAdmin.class) - public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) { - return new RabbitAdmin(connectionFactory); - } - - @Autowired - private ConnectionFactory connectionFactory; - - @Autowired - private ObjectProvider messageConverter; - - @Bean - @ConditionalOnMissingBean(RabbitTemplate.class) - public RabbitTemplate rabbitTemplate(RabbitProperties config) { - RabbitTemplate rabbitTemplate = new RabbitTemplate(this.connectionFactory); - MessageConverter messageConverter = this.messageConverter.getIfUnique(); - if (messageConverter != null) { - rabbitTemplate.setMessageConverter(messageConverter); - } - Template template = config.getTemplate(); - Retry retry = template.getRetry(); - if (retry.isEnabled()) { - RetryTemplate retryTemplate = new RetryTemplate(); - SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); - retryPolicy.setMaxAttempts(retry.getMaxAttempts()); - retryTemplate.setRetryPolicy(retryPolicy); - ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); - backOffPolicy.setInitialInterval(retry.getInitialInterval()); - backOffPolicy.setMultiplier(retry.getMultiplier()); - backOffPolicy.setMaxInterval(retry.getMaxInterval()); - retryTemplate.setBackOffPolicy(backOffPolicy); - rabbitTemplate.setRetryTemplate(retryTemplate); - } - if (template.getReceiveTimeout() != null) { - rabbitTemplate.setReceiveTimeout(template.getReceiveTimeout()); - } - if (template.getReplyTimeout() != null) { - rabbitTemplate.setReplyTimeout(template.getReplyTimeout()); - } - return rabbitTemplate; - } - @Configuration @ConditionalOnMissingBean(ConnectionFactory.class) protected static class RabbitConnectionFactoryCreator { @@ -185,11 +142,67 @@ public class RabbitAutoConfiguration { } + @Configuration + @Import(RabbitConnectionFactoryCreator.class) + protected static class RabbitTemplateConfiguration { + + @Autowired + private ObjectProvider messageConverter; + + @Autowired + private RabbitProperties properties; + + @Bean + @ConditionalOnSingleCandidate(ConnectionFactory.class) + @ConditionalOnMissingBean(RabbitTemplate.class) + public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { + RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); + MessageConverter messageConverter = this.messageConverter.getIfUnique(); + if (messageConverter != null) { + rabbitTemplate.setMessageConverter(messageConverter); + } + Template template = this.properties.getTemplate(); + Retry retry = template.getRetry(); + if (retry.isEnabled()) { + RetryTemplate retryTemplate = new RetryTemplate(); + SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); + retryPolicy.setMaxAttempts(retry.getMaxAttempts()); + retryTemplate.setRetryPolicy(retryPolicy); + ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); + backOffPolicy.setInitialInterval(retry.getInitialInterval()); + backOffPolicy.setMultiplier(retry.getMultiplier()); + backOffPolicy.setMaxInterval(retry.getMaxInterval()); + retryTemplate.setBackOffPolicy(backOffPolicy); + rabbitTemplate.setRetryTemplate(retryTemplate); + } + if (template.getReceiveTimeout() != null) { + rabbitTemplate.setReceiveTimeout(template.getReceiveTimeout()); + } + if (template.getReplyTimeout() != null) { + rabbitTemplate.setReplyTimeout(template.getReplyTimeout()); + } + return rabbitTemplate; + } + + @Bean + @ConditionalOnSingleCandidate(ConnectionFactory.class) + @ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true) + @ConditionalOnMissingBean(AmqpAdmin.class) + public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) { + return new RabbitAdmin(connectionFactory); + } + + + } + + @Configuration @ConditionalOnClass(RabbitMessagingTemplate.class) @ConditionalOnMissingBean(RabbitMessagingTemplate.class) + @Import(RabbitTemplateConfiguration.class) protected static class MessagingTemplateConfiguration { @Bean + @ConditionalOnSingleCandidate(RabbitTemplate.class) public RabbitMessagingTemplate rabbitMessagingTemplate( RabbitTemplate rabbitTemplate) { return new RabbitMessagingTemplate(rabbitTemplate); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration.java index 72d5f5eaac..b302c6b753 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration.java @@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -46,39 +47,44 @@ import org.springframework.jms.support.destination.DestinationResolver; @Import(JmsAnnotationDrivenConfiguration.class) public class JmsAutoConfiguration { - @Autowired - private JmsProperties properties; + @Configuration + protected static class JmsTemplateConfiguration { - @Autowired - private ConnectionFactory connectionFactory; + @Autowired + private JmsProperties properties; - @Autowired - private ObjectProvider destinationResolver; + @Autowired + private ObjectProvider destinationResolver; - @Autowired - private ObjectProvider messageConverter; + @Autowired + private ObjectProvider messageConverter; + + @Bean + @ConditionalOnMissingBean + @ConditionalOnSingleCandidate(ConnectionFactory.class) + public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) { + JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); + jmsTemplate.setPubSubDomain(this.properties.isPubSubDomain()); + DestinationResolver destinationResolver = this.destinationResolver.getIfUnique(); + if (destinationResolver != null) { + jmsTemplate.setDestinationResolver(destinationResolver); + } + MessageConverter messageConverter = this.messageConverter.getIfUnique(); + if (messageConverter != null) { + jmsTemplate.setMessageConverter(messageConverter); + } + return jmsTemplate; - @Bean - @ConditionalOnMissingBean - public JmsTemplate jmsTemplate() { - JmsTemplate jmsTemplate = new JmsTemplate(this.connectionFactory); - jmsTemplate.setPubSubDomain(this.properties.isPubSubDomain()); - DestinationResolver destinationResolver = this.destinationResolver.getIfUnique(); - if (destinationResolver != null) { - jmsTemplate.setDestinationResolver(destinationResolver); } - MessageConverter messageConverter = this.messageConverter.getIfUnique(); - if (messageConverter != null) { - jmsTemplate.setMessageConverter(messageConverter); - } - return jmsTemplate; } @ConditionalOnClass(JmsMessagingTemplate.class) - @ConditionalOnMissingBean(JmsMessagingTemplate.class) + @Import(JmsTemplateConfiguration.class) protected static class MessagingTemplateConfiguration { @Bean + @ConditionalOnMissingBean + @ConditionalOnSingleCandidate(JmsTemplate.class) public JmsMessagingTemplate jmsMessagingTemplate(JmsTemplate jmsTemplate) { return new JmsMessagingTemplate(jmsTemplate); }