Skip to content

Commit 8f06d81

Browse files
committed
Harmonize JMS connection factory bean names
This commit restores consistent naming for the auto-configured connection factory. Closes gh-19565
1 parent d1f4a18 commit 8f06d81

File tree

6 files changed

+75
-49
lines changed

6 files changed

+75
-49
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JndiConnectionFactoryAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ public class JndiConnectionFactoryAutoConfiguration {
5555
private static final String[] JNDI_LOCATIONS = { "java:/JmsXA", "java:/XAConnectionFactory" };
5656

5757
@Bean
58-
public ConnectionFactory connectionFactory(JmsProperties properties) throws NamingException {
58+
public ConnectionFactory jmsConnectionFactory(JmsProperties properties) throws NamingException {
5959
JndiLocatorDelegate jndiLocatorDelegate = JndiLocatorDelegate.createDefaultResourceRefLocator();
6060
if (StringUtils.hasLength(properties.getJndiName())) {
6161
return jndiLocatorDelegate.lookup(properties.getJndiName(), ConnectionFactory.class);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,8 +73,7 @@ private static ActiveMQConnectionFactory createJmsConnectionFactory(ActiveMQProp
7373
static class CachingConnectionFactoryConfiguration {
7474

7575
@Bean
76-
CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties,
77-
ActiveMQProperties properties,
76+
CachingConnectionFactory jmsConnectionFactory(JmsProperties jmsProperties, ActiveMQProperties properties,
7877
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
7978
JmsProperties.Cache cacheProperties = jmsProperties.getCache();
8079
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
@@ -95,7 +94,7 @@ static class PooledConnectionFactoryConfiguration {
9594

9695
@Bean(destroyMethod = "stop")
9796
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true")
98-
JmsPoolConnectionFactory pooledJmsConnectionFactory(ActiveMQProperties properties,
97+
JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
9998
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
10099
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties,
101100
factoryCustomizers.orderedStream().collect(Collectors.toList()))

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ static class SimpleConnectionFactoryConfiguration {
5858
this.beanFactory = beanFactory;
5959
}
6060

61-
@Bean
61+
@Bean(name = "jmsConnectionFactory")
6262
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true",
6363
matchIfMissing = true)
6464
CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties) {
@@ -70,7 +70,7 @@ CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties
7070
return connectionFactory;
7171
}
7272

73-
@Bean
73+
@Bean(name = "jmsConnectionFactory")
7474
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
7575
ActiveMQConnectionFactory jmsConnectionFactory() {
7676
return createConnectionFactory();
@@ -85,13 +85,11 @@ private ActiveMQConnectionFactory createConnectionFactory() {
8585

8686
@Configuration(proxyBeanMethods = false)
8787
@ConditionalOnClass({ JmsPoolConnectionFactory.class, PooledObject.class })
88+
@ConditionalOnProperty(prefix = "spring.artemis.pool", name = "enabled", havingValue = "true")
8889
static class PooledConnectionFactoryConfiguration {
8990

9091
@Bean(destroyMethod = "stop")
91-
@ConditionalOnProperty(prefix = "spring.artemis.pool", name = "enabled", havingValue = "true",
92-
matchIfMissing = false)
93-
JmsPoolConnectionFactory pooledJmsConnectionFactory(ListableBeanFactory beanFactory,
94-
ArtemisProperties properties) {
92+
JmsPoolConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties) {
9593
ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties)
9694
.createConnectionFactory(ActiveMQConnectionFactory.class);
9795
return new JmsPoolConnectionFactoryFactory(properties.getPool())

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JndiConnectionFactoryAutoConfigurationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,8 +111,9 @@ void jndiNamePropertySetWithWrongValue() {
111111

112112
private ContextConsumer<AssertableApplicationContext> assertConnectionFactory(ConnectionFactory connectionFactory) {
113113
return (context) -> {
114-
assertThat(context).hasSingleBean(ConnectionFactory.class);
115-
assertThat(context.getBean(ConnectionFactory.class)).isSameAs(connectionFactory);
114+
assertThat(context).hasSingleBean(ConnectionFactory.class).hasBean("jmsConnectionFactory");
115+
assertThat(context.getBean(ConnectionFactory.class)).isSameAs(connectionFactory)
116+
.isSameAs(context.getBean("jmsConnectionFactory"));
116117
};
117118
}
118119

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQAutoConfigurationTests.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,13 +50,12 @@ class ActiveMQAutoConfigurationTests {
5050
@Test
5151
void brokerIsEmbeddedByDefault() {
5252
this.contextRunner.withUserConfiguration(EmptyConfiguration.class).run((context) -> {
53-
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
54-
CachingConnectionFactory cachingConnectionFactory = context.getBean(CachingConnectionFactory.class);
55-
assertThat(cachingConnectionFactory.getTargetConnectionFactory())
56-
.isInstanceOf(ActiveMQConnectionFactory.class);
57-
assertThat(
58-
((ActiveMQConnectionFactory) cachingConnectionFactory.getTargetConnectionFactory()).getBrokerURL())
59-
.isEqualTo("vm://localhost?broker.persistent=false");
53+
assertThat(context).hasSingleBean(CachingConnectionFactory.class).hasBean("jmsConnectionFactory");
54+
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
55+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
56+
assertThat(connectionFactory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
57+
assertThat(((ActiveMQConnectionFactory) connectionFactory.getTargetConnectionFactory()).getBrokerURL())
58+
.isEqualTo("vm://localhost?broker.persistent=false");
6059
});
6160
}
6261

@@ -69,9 +68,10 @@ void configurationBacksOffWhenCustomConnectionFactoryExists() {
6968
@Test
7069
void connectionFactoryIsCachedByDefault() {
7170
this.contextRunner.withUserConfiguration(EmptyConfiguration.class).run((context) -> {
72-
assertThat(context).hasSingleBean(ConnectionFactory.class);
73-
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
71+
assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(CachingConnectionFactory.class)
72+
.hasBean("jmsConnectionFactory");
7473
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
74+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
7575
assertThat(connectionFactory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
7676
assertThat(connectionFactory.isCacheConsumers()).isFalse();
7777
assertThat(connectionFactory.isCacheProducers()).isTrue();
@@ -85,9 +85,10 @@ void connectionFactoryCachingCanBeCustomized() {
8585
.withPropertyValues("spring.jms.cache.consumers=true", "spring.jms.cache.producers=false",
8686
"spring.jms.cache.session-cache-size=10")
8787
.run((context) -> {
88-
assertThat(context).hasSingleBean(ConnectionFactory.class);
89-
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
88+
assertThat(context).hasSingleBean(ConnectionFactory.class)
89+
.hasSingleBean(CachingConnectionFactory.class).hasBean("jmsConnectionFactory");
9090
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
91+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
9192
assertThat(connectionFactory.isCacheConsumers()).isTrue();
9293
assertThat(connectionFactory.isCacheProducers()).isFalse();
9394
assertThat(connectionFactory.getSessionCacheSize()).isEqualTo(10);
@@ -98,8 +99,10 @@ void connectionFactoryCachingCanBeCustomized() {
9899
void connectionFactoryCachingCanBeDisabled() {
99100
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
100101
.withPropertyValues("spring.jms.cache.enabled=false").run((context) -> {
101-
assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class)).hasSize(1);
102+
assertThat(context).hasSingleBean(ConnectionFactory.class)
103+
.hasSingleBean(ActiveMQConnectionFactory.class).hasBean("jmsConnectionFactory");
102104
ActiveMQConnectionFactory connectionFactory = context.getBean(ActiveMQConnectionFactory.class);
105+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
103106
ActiveMQConnectionFactory defaultFactory = new ActiveMQConnectionFactory(
104107
"vm://localhost?broker.persistent=false");
105108
assertThat(connectionFactory.getUserName()).isEqualTo(defaultFactory.getUserName());
@@ -123,8 +126,10 @@ void customConnectionFactoryIsApplied() {
123126
"spring.activemq.nonBlockingRedelivery=true", "spring.activemq.sendTimeout=1000",
124127
"spring.activemq.packages.trust-all=false", "spring.activemq.packages.trusted=com.example.acme")
125128
.run((context) -> {
126-
assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class)).hasSize(1);
129+
assertThat(context).hasSingleBean(ConnectionFactory.class)
130+
.hasSingleBean(ActiveMQConnectionFactory.class).hasBean("jmsConnectionFactory");
127131
ActiveMQConnectionFactory connectionFactory = context.getBean(ActiveMQConnectionFactory.class);
132+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
128133
assertThat(connectionFactory.getUserName()).isEqualTo("foo");
129134
assertThat(connectionFactory.getPassword()).isEqualTo("bar");
130135
assertThat(connectionFactory.getCloseTimeout()).isEqualTo(500);
@@ -139,8 +144,10 @@ void customConnectionFactoryIsApplied() {
139144
void defaultPoolConnectionFactoryIsApplied() {
140145
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
141146
.withPropertyValues("spring.activemq.pool.enabled=true").run((context) -> {
142-
assertThat(context.getBeansOfType(JmsPoolConnectionFactory.class)).hasSize(1);
147+
assertThat(context).hasSingleBean(ConnectionFactory.class)
148+
.hasSingleBean(JmsPoolConnectionFactory.class).hasBean("jmsConnectionFactory");
143149
JmsPoolConnectionFactory connectionFactory = context.getBean(JmsPoolConnectionFactory.class);
150+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
144151
JmsPoolConnectionFactory defaultFactory = new JmsPoolConnectionFactory();
145152
assertThat(connectionFactory.isBlockIfSessionPoolIsFull())
146153
.isEqualTo(defaultFactory.isBlockIfSessionPoolIsFull());
@@ -167,8 +174,10 @@ void customPoolConnectionFactoryIsApplied() {
167174
"spring.activemq.pool.timeBetweenExpirationCheck=2048",
168175
"spring.activemq.pool.useAnonymousProducers=false")
169176
.run((context) -> {
170-
assertThat(context.getBeansOfType(JmsPoolConnectionFactory.class)).hasSize(1);
177+
assertThat(context).hasSingleBean(ConnectionFactory.class)
178+
.hasSingleBean(JmsPoolConnectionFactory.class).hasBean("jmsConnectionFactory");
171179
JmsPoolConnectionFactory connectionFactory = context.getBean(JmsPoolConnectionFactory.class);
180+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
172181
assertThat(connectionFactory.isBlockIfSessionPoolIsFull()).isFalse();
173182
assertThat(connectionFactory.getBlockIfSessionPoolIsFullTimeout()).isEqualTo(64);
174183
assertThat(connectionFactory.getConnectionIdleTimeout()).isEqualTo(512);
@@ -183,7 +192,10 @@ void customPoolConnectionFactoryIsApplied() {
183192
void poolConnectionFactoryConfiguration() {
184193
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
185194
.withPropertyValues("spring.activemq.pool.enabled:true").run((context) -> {
195+
assertThat(context).hasSingleBean(ConnectionFactory.class)
196+
.hasSingleBean(JmsPoolConnectionFactory.class).hasBean("jmsConnectionFactory");
186197
ConnectionFactory factory = context.getBean(ConnectionFactory.class);
198+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(factory);
187199
assertThat(factory).isInstanceOf(JmsPoolConnectionFactory.class);
188200
context.getSourceApplicationContext().close();
189201
assertThat(factory.createConnection()).isNull();
@@ -194,14 +206,20 @@ void poolConnectionFactoryConfiguration() {
194206
void cachingConnectionFactoryNotOnTheClasspathThenSimpleConnectionFactoryAutoConfigured() {
195207
this.contextRunner.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class))
196208
.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=false")
197-
.run((context) -> assertThat(context).hasSingleBean(ActiveMQConnectionFactory.class));
209+
.run((context) -> {
210+
assertThat(context).hasSingleBean(ConnectionFactory.class)
211+
.hasSingleBean(ActiveMQConnectionFactory.class).hasBean("jmsConnectionFactory");
212+
ActiveMQConnectionFactory connectionFactory = context.getBean(ActiveMQConnectionFactory.class);
213+
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
214+
});
198215
}
199216

200217
@Test
201218
void cachingConnectionFactoryNotOnTheClasspathAndCacheEnabledThenSimpleConnectionFactoryNotConfigured() {
202219
this.contextRunner.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class))
203220
.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=true")
204-
.run((context) -> assertThat(context).doesNotHaveBean(ActiveMQConnectionFactory.class));
221+
.run((context) -> assertThat(context).doesNotHaveBean(ConnectionFactory.class)
222+
.doesNotHaveBean(ActiveMQConnectionFactory.class).doesNotHaveBean("jmsConnectionFactory"));
205223
}
206224

207225
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)