Refactor Rabbit and JMS to avoid bean name clash

pull/64/head
Dave Syer 11 years ago
parent 923f286ae2
commit 52e0193d95

@ -23,6 +23,7 @@ import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateAutoConfiguration.RabbitConnectionFactoryProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -31,14 +32,16 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.rabbitmq.client.Channel;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
*
* @author Greg Turnquist
*/
@Configuration
@ConditionalOnClass({ RabbitTemplate.class })
@EnableConfigurationProperties
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
public class RabbitTemplateAutoConfiguration {
@Bean
@ -48,38 +51,30 @@ public class RabbitTemplateAutoConfiguration {
return new RabbitAdmin(connectionFactory);
}
@Configuration
@ConditionalOnMissingBean(RabbitTemplate.class)
protected static class RabbitTemplateCreator {
@Autowired
CachingConnectionFactory connectionFactory;
private ConnectionFactory connectionFactory;
@Bean
@ConditionalOnMissingBean(RabbitTemplate.class)
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(this.connectionFactory);
}
}
@Configuration
@ConditionalOnMissingBean(ConnectionFactory.class)
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
protected static class RabbitConnectionFactoryCreator {
@Autowired
private RabbitConnectionFactoryProperties config;
@Bean
public CachingConnectionFactory connectionFactory() {
public ConnectionFactory rabbitConnectionFactory(
RabbitConnectionFactoryProperties config) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
this.config.getHost());
connectionFactory.setPort(this.config.getPort());
if (this.config.getUsername() != null) {
connectionFactory.setUsername(this.config.getUsername());
config.getHost());
connectionFactory.setPort(config.getPort());
if (config.getUsername() != null) {
connectionFactory.setUsername(config.getUsername());
}
if (this.config.getPassword() != null) {
connectionFactory.setPassword(this.config.getPassword());
if (config.getPassword() != null) {
connectionFactory.setPassword(config.getPassword());
}
return connectionFactory;
}

@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration.JmsTemplateProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -37,13 +38,9 @@ import org.springframework.jms.core.JmsTemplate;
*/
@Configuration
@ConditionalOnClass({ JmsTemplate.class, ConnectionFactory.class })
@EnableConfigurationProperties(JmsTemplateProperties.class)
public class JmsTemplateAutoConfiguration {
@Configuration
@ConditionalOnMissingBean(JmsTemplate.class)
@EnableConfigurationProperties(JmsTemplateProperties.class)
protected static class JmsTemplateCreator {
@Autowired
private JmsTemplateProperties config;
@ -51,21 +48,20 @@ public class JmsTemplateAutoConfiguration {
private ConnectionFactory connectionFactory;
@Bean
@ConditionalOnMissingBean(JmsTemplate.class)
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate(this.connectionFactory);
jmsTemplate.setPubSubDomain(this.config.isPubSubDomain());
return jmsTemplate;
}
}
@ConfigurationProperties(name = "spring.jms")
public static class JmsTemplateProperties {
private boolean pubSubDomain = true;
public boolean isPubSubDomain() {
return pubSubDomain;
return this.pubSubDomain;
}
public void setPubSubDomain(boolean pubSubDomain) {
@ -84,12 +80,14 @@ public class JmsTemplateAutoConfiguration {
private ActiveMQConnectionFactoryProperties config;
@Bean
ConnectionFactory connectionFactory() {
ConnectionFactory jmsConnectionFactory() {
if (this.config.isPooled()) {
PooledConnectionFactory pool = new PooledConnectionFactory();
pool.setConnectionFactory(new ActiveMQConnectionFactory(this.config.getBrokerURL()));
pool.setConnectionFactory(new ActiveMQConnectionFactory(this.config
.getBrokerURL()));
return pool;
} else {
}
else {
return new ActiveMQConnectionFactory(this.config.getBrokerURL());
}
}
@ -109,7 +107,8 @@ public class JmsTemplateAutoConfiguration {
public String getBrokerURL() {
if (this.inMemory) {
return "vm://localhost";
} else {
}
else {
return this.brokerURL;
}
}
@ -119,7 +118,7 @@ public class JmsTemplateAutoConfiguration {
}
public boolean isInMemory() {
return inMemory;
return this.inMemory;
}
public void setInMemory(boolean inMemory) {
@ -127,7 +126,7 @@ public class JmsTemplateAutoConfiguration {
}
public boolean isPooled() {
return pooled;
return this.pooled;
}
public void setPooled(boolean pooled) {

Loading…
Cancel
Save