From c4205d04b30cad4390b2f3c21d198b4181c9d957 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 23 Feb 2016 14:22:54 +0100 Subject: [PATCH] Rework ListenerContainerFactory configurers Rework commit b726974 to avoid exposing setters that would permit anyone to change Spring Boot's defaults. Also, since these are configurers of a specific instance, they should be named accordingly. Closes gh-5138 --- .../RabbitAnnotationDrivenConfiguration.java | 18 ++++++++--- ...itListenerContainerFactoryConfigurer.java} | 23 +++----------- ...msListenerContainerFactoryConfigurer.java} | 31 +++++-------------- .../jms/JmsAnnotationDrivenConfiguration.java | 26 +++++++++++++--- .../jms/JmsAutoConfigurationTests.java | 7 +++-- .../main/asciidoc/spring-boot-features.adoc | 24 +++++++------- 6 files changed, 64 insertions(+), 65 deletions(-) rename spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/{RabbitListenerContainerFactoryConfigurer.java => SimpleRabbitListenerContainerFactoryConfigurer.java} (72%) rename spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/{JmsListenerContainerFactoryConfigurer.java => DefaultJmsListenerContainerFactoryConfigurer.java} (70%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java index 8c94af415b..c17b8d7d18 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAnnotationDrivenConfiguration.java @@ -20,6 +20,7 @@ import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; @@ -36,18 +37,27 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnClass(EnableRabbit.class) class RabbitAnnotationDrivenConfiguration { + @Autowired + private RabbitProperties properties; + @Bean @ConditionalOnMissingBean - public RabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer() { - return new RabbitListenerContainerFactoryConfigurer(); + public SimpleRabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer() { + SimpleRabbitListenerContainerFactoryConfigurer configurer = + new SimpleRabbitListenerContainerFactoryConfigurer(); + configurer.setRabbitProperties(this.properties); + return configurer; } @Bean @ConditionalOnMissingBean(name = "rabbitListenerContainerFactory") public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory( - RabbitListenerContainerFactoryConfigurer configurer, + SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { - return configurer.createRabbitListenerContainerFactory(connectionFactory); + SimpleRabbitListenerContainerFactory factory = + new SimpleRabbitListenerContainerFactory(); + configurer.configure(factory, connectionFactory); + return factory; } @EnableRabbit diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitListenerContainerFactoryConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/SimpleRabbitListenerContainerFactoryConfigurer.java similarity index 72% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitListenerContainerFactoryConfigurer.java rename to spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/SimpleRabbitListenerContainerFactoryConfigurer.java index b5e36c83fa..679733b3c2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitListenerContainerFactoryConfigurer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/SimpleRabbitListenerContainerFactoryConfigurer.java @@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.amqp; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.Assert; /** @@ -28,7 +27,7 @@ import org.springframework.util.Assert; * @author Stephane Nicoll * @since 1.3.3 */ -public final class RabbitListenerContainerFactoryConfigurer { +public final class SimpleRabbitListenerContainerFactoryConfigurer { private RabbitProperties rabbitProperties; @@ -36,27 +35,13 @@ public final class RabbitListenerContainerFactoryConfigurer { * Set the {@link RabbitProperties} to use. * @param rabbitProperties the {@link RabbitProperties} */ - @Autowired - public void setRabbitProperties(RabbitProperties rabbitProperties) { + void setRabbitProperties(RabbitProperties rabbitProperties) { this.rabbitProperties = rabbitProperties; } /** - * Create a new and pre-configured {@link SimpleRabbitListenerContainerFactory} - * instance for the specified {@link ConnectionFactory}. - * @param connectionFactory the {@link ConnectionFactory} to use. - * @return a pre-configured {@link SimpleRabbitListenerContainerFactory} - */ - public SimpleRabbitListenerContainerFactory createRabbitListenerContainerFactory( - ConnectionFactory connectionFactory) { - SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); - configure(factory, connectionFactory); - return factory; - } - - /** - * Apply the default settings for the specified jms listener container factory. The - * factory can be further tuned and default settings can be overridden. + * Configure the specified rabbit listener container factory. The factory can be + * further tuned and default settings can be overridden. * @param factory the {@link SimpleRabbitListenerContainerFactory} instance to * configure * @param connectionFactory the {@link ConnectionFactory} to use diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsListenerContainerFactoryConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java similarity index 70% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsListenerContainerFactoryConfigurer.java rename to spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java index ae4f739ee9..4655bf4762 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsListenerContainerFactoryConfigurer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java @@ -18,20 +18,18 @@ package org.springframework.boot.autoconfigure.jms; import javax.jms.ConnectionFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; -import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.support.destination.DestinationResolver; import org.springframework.transaction.jta.JtaTransactionManager; import org.springframework.util.Assert; /** - * Configure {@link JmsListenerContainerFactory} with sensible defaults. + * Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults. * * @author Stephane Nicoll * @since 1.3.3 */ -public final class JmsListenerContainerFactoryConfigurer { +public final class DefaultJmsListenerContainerFactoryConfigurer { private DestinationResolver destinationResolver; @@ -44,8 +42,7 @@ public final class JmsListenerContainerFactoryConfigurer { * resolver should be associated with the factory by default. * @param destinationResolver the {@link DestinationResolver} */ - @Autowired(required = false) - public void setDestinationResolver(DestinationResolver destinationResolver) { + void setDestinationResolver(DestinationResolver destinationResolver) { this.destinationResolver = destinationResolver; } @@ -54,8 +51,7 @@ public final class JmsListenerContainerFactoryConfigurer { * should not be used. * @param transactionManager the {@link JtaTransactionManager} */ - @Autowired(required = false) - public void setTransactionManager(JtaTransactionManager transactionManager) { + void setTransactionManager(JtaTransactionManager transactionManager) { this.transactionManager = transactionManager; } @@ -63,27 +59,14 @@ public final class JmsListenerContainerFactoryConfigurer { * Set the {@link JmsProperties to use}. * @param jmsProperties the {@link JmsProperties} */ - @Autowired - public void setJmsProperties(JmsProperties jmsProperties) { + void setJmsProperties(JmsProperties jmsProperties) { this.jmsProperties = jmsProperties; } - /** - * Create a new and pre-configured {@link DefaultJmsListenerContainerFactory} instance - * for the specified {@link ConnectionFactory}. - * @param connectionFactory the {@link ConnectionFactory} to use. - * @return a pre-configured {@link DefaultJmsListenerContainerFactory} - */ - public DefaultJmsListenerContainerFactory createJmsListenerContainerFactory( - ConnectionFactory connectionFactory) { - DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); - configure(factory, connectionFactory); - return factory; - } /** - * Apply the default settings for the specified jms listener container factory. The - * factory can be further tuned and default settings can be overridden. + * Configure the specified jms listener container factory. The factory can be further + * tuned and default settings can be overridden. * @param factory the {@link DefaultJmsListenerContainerFactory} instance to configure * @param connectionFactory the {@link ConnectionFactory} to use */ diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAnnotationDrivenConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAnnotationDrivenConfiguration.java index 3c953b77b1..ff27e40632 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAnnotationDrivenConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAnnotationDrivenConfiguration.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jms; import javax.jms.ConnectionFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnJndi; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -28,6 +29,7 @@ import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerConfigUtils; import org.springframework.jms.support.destination.DestinationResolver; import org.springframework.jms.support.destination.JndiDestinationResolver; +import org.springframework.transaction.jta.JtaTransactionManager; /** * Configuration for Spring 4.1 annotation driven JMS. @@ -40,18 +42,34 @@ import org.springframework.jms.support.destination.JndiDestinationResolver; @ConditionalOnClass(EnableJms.class) class JmsAnnotationDrivenConfiguration { + @Autowired(required = false) + private DestinationResolver destinationResolver; + + @Autowired(required = false) + private JtaTransactionManager transactionManager; + + @Autowired + private JmsProperties properties; + @Bean @ConditionalOnMissingBean - public JmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigurer() { - return new JmsListenerContainerFactoryConfigurer(); + public DefaultJmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigurer() { + DefaultJmsListenerContainerFactoryConfigurer configurer = + new DefaultJmsListenerContainerFactoryConfigurer(); + configurer.setDestinationResolver(this.destinationResolver); + configurer.setTransactionManager(this.transactionManager); + configurer.setJmsProperties(this.properties); + return configurer; } @Bean @ConditionalOnMissingBean(name = "jmsListenerContainerFactory") public DefaultJmsListenerContainerFactory jmsListenerContainerFactory( - JmsListenerContainerFactoryConfigurer configurer, + DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { - return configurer.createJmsListenerContainerFactory(connectionFactory); + DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); + configurer.configure(factory, connectionFactory); + return factory; } @EnableJms diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java index caa6813273..efd134b93f 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java @@ -469,10 +469,11 @@ public class JmsAutoConfigurationTests { @Bean JmsListenerContainerFactory customListenerContainerFactory( - JmsListenerContainerFactoryConfigurer configurer, + DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { - DefaultJmsListenerContainerFactory factory = configurer - .createJmsListenerContainerFactory(connectionFactory); + DefaultJmsListenerContainerFactory factory = + new DefaultJmsListenerContainerFactory(); + configurer.configure(factory, connectionFactory); factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER); return factory; diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 1285c38079..3237f78ea1 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -3490,9 +3490,9 @@ TIP: Check {spring-javadoc}/jms/annotation/EnableJms.{dc-ext}[the Javadoc of `@E more details. If you need to create more `JmsListenerContainerFactory` instances or if you want to override -the default, Spring Boot provides a `JmsListenerContainerFactoryConfigurer` that you can use -to initialize a `DefaultJmsListenerContainerFactory` with the same settings as the one that -is auto-configured. +the default, Spring Boot provides a `DefaultJmsListenerContainerFactoryConfigurer` that you +can use to initialize a `DefaultJmsListenerContainerFactory` with the same settings as the one +that is auto-configured. For instance, the following exposes another factory that uses a specific `MessageConverter`: @@ -3503,9 +3503,10 @@ For instance, the following exposes another factory that uses a specific `Messag @Bean public DefaultJmsListenerContainerFactory myFactory( - JmsListenerContainerFactoryConfigurer configurer) { - DefaultJmsListenerContainerFactory factory = configurer - .createJmsListenerContainerFactory(connectionFactory()); + DefaultJmsListenerContainerFactoryConfigurer configurer) { + DefaultJmsListenerContainerFactory factory = + new DefaultJmsListenerContainerFactory(); + configurer.configure(factory, connectionFactory()); factory.setMessageConverter(myMessageConverter()); return factory; } @@ -3624,8 +3625,8 @@ TIP: Check {spring-amqp-javadoc}/rabbit/annotation/EnableRabbit.{dc-ext}[the Jav for more details. If you need to create more `RabbitListenerContainerFactory` instances or if you want to override -the default, Spring Boot provides a `RabbitListenerContainerFactoryConfigurer` that you can use -to initialize a `SimpleRabbitListenerContainerFactory` with the same settings as the one that +the default, Spring Boot provides a `SimpleRabbitListenerContainerFactoryConfigurer` that you can +use to initialize a `SimpleRabbitListenerContainerFactory` with the same settings as the one that is auto-configured. For instance, the following exposes another factory that uses a specific `MessageConverter`: @@ -3637,9 +3638,10 @@ For instance, the following exposes another factory that uses a specific `Messag @Bean public SimpleRabbitListenerContainerFactory myFactory( - RabbitListenerContainerFactoryConfigurer configurer) { - SimpleRabbitListenerContainerFactory factory = configurer - .createRabbitListenerContainerFactory(connectionFactory()); + SimpleRabbitListenerContainerFactoryConfigurer configurer) { + SimpleRabbitListenerContainerFactory factory = + new SimpleRabbitListenerContainerFactory(); + configurer.configure(factory, connectionFactory); factory.setMessageConverter(myMessageConverter()); return factory; }