mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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
This commit is contained in:
parent
f94e8bd287
commit
c4205d04b3
@ -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
|
||||
|
@ -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
|
@ -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
|
||||
*/
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user