Commit c4205d04 authored by Stephane Nicoll's avatar Stephane Nicoll

Rework ListenerContainerFactory configurers

Rework commit b726974b 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
parent f94e8bd2
...@@ -20,6 +20,7 @@ import org.springframework.amqp.rabbit.annotation.EnableRabbit; ...@@ -20,6 +20,7 @@ import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils; import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory; 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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -36,18 +37,27 @@ import org.springframework.context.annotation.Configuration; ...@@ -36,18 +37,27 @@ import org.springframework.context.annotation.Configuration;
@ConditionalOnClass(EnableRabbit.class) @ConditionalOnClass(EnableRabbit.class)
class RabbitAnnotationDrivenConfiguration { class RabbitAnnotationDrivenConfiguration {
@Autowired
private RabbitProperties properties;
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer() { public SimpleRabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer() {
return new RabbitListenerContainerFactoryConfigurer(); SimpleRabbitListenerContainerFactoryConfigurer configurer =
new SimpleRabbitListenerContainerFactoryConfigurer();
configurer.setRabbitProperties(this.properties);
return configurer;
} }
@Bean @Bean
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory") @ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory( public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
RabbitListenerContainerFactoryConfigurer configurer, SimpleRabbitListenerContainerFactoryConfigurer configurer,
ConnectionFactory connectionFactory) { ConnectionFactory connectionFactory) {
return configurer.createRabbitListenerContainerFactory(connectionFactory); SimpleRabbitListenerContainerFactory factory =
new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
} }
@EnableRabbit @EnableRabbit
......
...@@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.amqp; ...@@ -19,7 +19,6 @@ package org.springframework.boot.autoconfigure.amqp;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
...@@ -28,7 +27,7 @@ import org.springframework.util.Assert; ...@@ -28,7 +27,7 @@ import org.springframework.util.Assert;
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.3.3 * @since 1.3.3
*/ */
public final class RabbitListenerContainerFactoryConfigurer { public final class SimpleRabbitListenerContainerFactoryConfigurer {
private RabbitProperties rabbitProperties; private RabbitProperties rabbitProperties;
...@@ -36,27 +35,13 @@ public final class RabbitListenerContainerFactoryConfigurer { ...@@ -36,27 +35,13 @@ public final class RabbitListenerContainerFactoryConfigurer {
* Set the {@link RabbitProperties} to use. * Set the {@link RabbitProperties} to use.
* @param rabbitProperties the {@link RabbitProperties} * @param rabbitProperties the {@link RabbitProperties}
*/ */
@Autowired void setRabbitProperties(RabbitProperties rabbitProperties) {
public void setRabbitProperties(RabbitProperties rabbitProperties) {
this.rabbitProperties = rabbitProperties; this.rabbitProperties = rabbitProperties;
} }
/** /**
* Create a new and pre-configured {@link SimpleRabbitListenerContainerFactory} * Configure the specified rabbit listener container factory. The factory can be
* instance for the specified {@link ConnectionFactory}. * further tuned and default settings can be overridden.
* @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.
* @param factory the {@link SimpleRabbitListenerContainerFactory} instance to * @param factory the {@link SimpleRabbitListenerContainerFactory} instance to
* configure * configure
* @param connectionFactory the {@link ConnectionFactory} to use * @param connectionFactory the {@link ConnectionFactory} to use
......
...@@ -18,20 +18,18 @@ package org.springframework.boot.autoconfigure.jms; ...@@ -18,20 +18,18 @@ package org.springframework.boot.autoconfigure.jms;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.support.destination.DestinationResolver; import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.transaction.jta.JtaTransactionManager; import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Configure {@link JmsListenerContainerFactory} with sensible defaults. * Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.3.3 * @since 1.3.3
*/ */
public final class JmsListenerContainerFactoryConfigurer { public final class DefaultJmsListenerContainerFactoryConfigurer {
private DestinationResolver destinationResolver; private DestinationResolver destinationResolver;
...@@ -44,8 +42,7 @@ public final class JmsListenerContainerFactoryConfigurer { ...@@ -44,8 +42,7 @@ public final class JmsListenerContainerFactoryConfigurer {
* resolver should be associated with the factory by default. * resolver should be associated with the factory by default.
* @param destinationResolver the {@link DestinationResolver} * @param destinationResolver the {@link DestinationResolver}
*/ */
@Autowired(required = false) void setDestinationResolver(DestinationResolver destinationResolver) {
public void setDestinationResolver(DestinationResolver destinationResolver) {
this.destinationResolver = destinationResolver; this.destinationResolver = destinationResolver;
} }
...@@ -54,8 +51,7 @@ public final class JmsListenerContainerFactoryConfigurer { ...@@ -54,8 +51,7 @@ public final class JmsListenerContainerFactoryConfigurer {
* should not be used. * should not be used.
* @param transactionManager the {@link JtaTransactionManager} * @param transactionManager the {@link JtaTransactionManager}
*/ */
@Autowired(required = false) void setTransactionManager(JtaTransactionManager transactionManager) {
public void setTransactionManager(JtaTransactionManager transactionManager) {
this.transactionManager = transactionManager; this.transactionManager = transactionManager;
} }
...@@ -63,27 +59,14 @@ public final class JmsListenerContainerFactoryConfigurer { ...@@ -63,27 +59,14 @@ public final class JmsListenerContainerFactoryConfigurer {
* Set the {@link JmsProperties to use}. * Set the {@link JmsProperties to use}.
* @param jmsProperties the {@link JmsProperties} * @param jmsProperties the {@link JmsProperties}
*/ */
@Autowired void setJmsProperties(JmsProperties jmsProperties) {
public void setJmsProperties(JmsProperties jmsProperties) {
this.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 * Configure the specified jms listener container factory. The factory can be further
* factory can be further tuned and default settings can be overridden. * tuned and default settings can be overridden.
* @param factory the {@link DefaultJmsListenerContainerFactory} instance to configure * @param factory the {@link DefaultJmsListenerContainerFactory} instance to configure
* @param connectionFactory the {@link ConnectionFactory} to use * @param connectionFactory the {@link ConnectionFactory} to use
*/ */
......
...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jms; ...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jms;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJndi; import org.springframework.boot.autoconfigure.condition.ConditionalOnJndi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
...@@ -28,6 +29,7 @@ import org.springframework.jms.config.DefaultJmsListenerContainerFactory; ...@@ -28,6 +29,7 @@ import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerConfigUtils; import org.springframework.jms.config.JmsListenerConfigUtils;
import org.springframework.jms.support.destination.DestinationResolver; import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.JndiDestinationResolver; import org.springframework.jms.support.destination.JndiDestinationResolver;
import org.springframework.transaction.jta.JtaTransactionManager;
/** /**
* Configuration for Spring 4.1 annotation driven JMS. * Configuration for Spring 4.1 annotation driven JMS.
...@@ -40,18 +42,34 @@ import org.springframework.jms.support.destination.JndiDestinationResolver; ...@@ -40,18 +42,34 @@ import org.springframework.jms.support.destination.JndiDestinationResolver;
@ConditionalOnClass(EnableJms.class) @ConditionalOnClass(EnableJms.class)
class JmsAnnotationDrivenConfiguration { class JmsAnnotationDrivenConfiguration {
@Autowired(required = false)
private DestinationResolver destinationResolver;
@Autowired(required = false)
private JtaTransactionManager transactionManager;
@Autowired
private JmsProperties properties;
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public JmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigurer() { public DefaultJmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigurer() {
return new JmsListenerContainerFactoryConfigurer(); DefaultJmsListenerContainerFactoryConfigurer configurer =
new DefaultJmsListenerContainerFactoryConfigurer();
configurer.setDestinationResolver(this.destinationResolver);
configurer.setTransactionManager(this.transactionManager);
configurer.setJmsProperties(this.properties);
return configurer;
} }
@Bean @Bean
@ConditionalOnMissingBean(name = "jmsListenerContainerFactory") @ConditionalOnMissingBean(name = "jmsListenerContainerFactory")
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory( public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
JmsListenerContainerFactoryConfigurer configurer, DefaultJmsListenerContainerFactoryConfigurer configurer,
ConnectionFactory connectionFactory) { ConnectionFactory connectionFactory) {
return configurer.createJmsListenerContainerFactory(connectionFactory); DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
} }
@EnableJms @EnableJms
......
...@@ -469,10 +469,11 @@ public class JmsAutoConfigurationTests { ...@@ -469,10 +469,11 @@ public class JmsAutoConfigurationTests {
@Bean @Bean
JmsListenerContainerFactory<?> customListenerContainerFactory( JmsListenerContainerFactory<?> customListenerContainerFactory(
JmsListenerContainerFactoryConfigurer configurer, DefaultJmsListenerContainerFactoryConfigurer configurer,
ConnectionFactory connectionFactory) { ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = configurer DefaultJmsListenerContainerFactory factory =
.createJmsListenerContainerFactory(connectionFactory); new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER); factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
return factory; return factory;
......
...@@ -3490,9 +3490,9 @@ TIP: Check {spring-javadoc}/jms/annotation/EnableJms.{dc-ext}[the Javadoc of `@E ...@@ -3490,9 +3490,9 @@ TIP: Check {spring-javadoc}/jms/annotation/EnableJms.{dc-ext}[the Javadoc of `@E
more details. more details.
If you need to create more `JmsListenerContainerFactory` instances or if you want to override 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 the default, Spring Boot provides a `DefaultJmsListenerContainerFactoryConfigurer` that you
to initialize a `DefaultJmsListenerContainerFactory` with the same settings as the one that can use to initialize a `DefaultJmsListenerContainerFactory` with the same settings as the one
is auto-configured. that is auto-configured.
For instance, the following exposes another factory that uses a specific `MessageConverter`: 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 ...@@ -3503,9 +3503,10 @@ For instance, the following exposes another factory that uses a specific `Messag
@Bean @Bean
public DefaultJmsListenerContainerFactory myFactory( public DefaultJmsListenerContainerFactory myFactory(
JmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = configurer DefaultJmsListenerContainerFactory factory =
.createJmsListenerContainerFactory(connectionFactory()); new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory());
factory.setMessageConverter(myMessageConverter()); factory.setMessageConverter(myMessageConverter());
return factory; return factory;
} }
...@@ -3624,8 +3625,8 @@ TIP: Check {spring-amqp-javadoc}/rabbit/annotation/EnableRabbit.{dc-ext}[the Jav ...@@ -3624,8 +3625,8 @@ TIP: Check {spring-amqp-javadoc}/rabbit/annotation/EnableRabbit.{dc-ext}[the Jav
for more details. for more details.
If you need to create more `RabbitListenerContainerFactory` instances or if you want to override 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 the default, Spring Boot provides a `SimpleRabbitListenerContainerFactoryConfigurer` that you can
to initialize a `SimpleRabbitListenerContainerFactory` with the same settings as the one that use to initialize a `SimpleRabbitListenerContainerFactory` with the same settings as the one that
is auto-configured. is auto-configured.
For instance, the following exposes another factory that uses a specific `MessageConverter`: 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 ...@@ -3637,9 +3638,10 @@ For instance, the following exposes another factory that uses a specific `Messag
@Bean @Bean
public SimpleRabbitListenerContainerFactory myFactory( public SimpleRabbitListenerContainerFactory myFactory(
RabbitListenerContainerFactoryConfigurer configurer) { SimpleRabbitListenerContainerFactoryConfigurer configurer) {
SimpleRabbitListenerContainerFactory factory = configurer SimpleRabbitListenerContainerFactory factory =
.createRabbitListenerContainerFactory(connectionFactory()); new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
factory.setMessageConverter(myMessageConverter()); factory.setMessageConverter(myMessageConverter());
return factory; return factory;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment