Skip to content

Commit 295ef65

Browse files
committed
Fixes: #3192 The `RabbitAdmin.initialize()` method calls `applicationContext.getBeansOfType(Declarable.class, false, true);` The third parameter `allowEagerInit = true` causes Spring to eagerly instantiate all `FactoryBean` instances in the application context — even those marked with `lazy-init="true"` — in order to determine their types. * Fix `RabbitAdmin`, `RabbitAmqpAdmin` and `RabbitListenerAnnotationBeanPostProcessor` to use `getBeansOfType(AClass.class, false, false)` to avoid eagerness for lazy-init **Auto-cherry-pick to `3.2.x`**
1 parent 4b10bc6 commit 295ef65

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/RabbitListenerAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public void afterSingletonsInstantiated() {
267267

268268
if (this.beanFactory instanceof ListableBeanFactory lbf) {
269269
Map<String, RabbitListenerConfigurer> instances =
270-
lbf.getBeansOfType(RabbitListenerConfigurer.class);
270+
lbf.getBeansOfType(RabbitListenerConfigurer.class, false, false);
271271
for (RabbitListenerConfigurer configurer : instances.values()) {
272272
configurer.configureRabbitListeners(this.registrar);
273273
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitAdmin.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,13 @@ private void redeclareBeanDeclarables() {
673673

674674
this.logger.debug("Initializing declarations");
675675
Collection<Exchange> contextExchanges = new LinkedList<>(
676-
this.applicationContext.getBeansOfType(Exchange.class).values());
676+
this.applicationContext.getBeansOfType(Exchange.class, false, false).values());
677677
Collection<Queue> contextQueues = new LinkedList<>(
678-
this.applicationContext.getBeansOfType(Queue.class).values());
678+
this.applicationContext.getBeansOfType(Queue.class, false, false).values());
679679
Collection<Binding> contextBindings = new LinkedList<>(
680-
this.applicationContext.getBeansOfType(Binding.class).values());
680+
this.applicationContext.getBeansOfType(Binding.class, false, false).values());
681681
Collection<DeclarableCustomizer> customizers =
682-
this.applicationContext.getBeansOfType(DeclarableCustomizer.class).values();
682+
this.applicationContext.getBeansOfType(DeclarableCustomizer.class, false, false).values();
683683

684684
processDeclarables(contextExchanges, contextQueues, contextBindings);
685685

@@ -768,7 +768,7 @@ private void processDeclarables(Collection<Exchange> contextExchanges, Collectio
768768
Collection<Binding> contextBindings) {
769769

770770
@SuppressWarnings("NullAway") // Dataflow analysis limitation
771-
Collection<Declarables> declarables = this.applicationContext.getBeansOfType(Declarables.class, false, true)
771+
Collection<Declarables> declarables = this.applicationContext.getBeansOfType(Declarables.class, false, false)
772772
.values();
773773
declarables.forEach(d -> {
774774
d.getDeclarables().forEach(declarable -> {

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitAdminTests.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void testFailOnFirstUseWithMissingBroker() {
131131
rabbitAdmin.setApplicationContext(applicationContext);
132132
rabbitAdmin.setAutoStartup(true);
133133
rabbitAdmin.afterPropertiesSet();
134-
assertThatIllegalArgumentException().isThrownBy(() -> rabbitAdmin.declareQueue());
134+
assertThatIllegalArgumentException().isThrownBy(rabbitAdmin::declareQueue);
135135
connectionFactory.destroy();
136136
}
137137

@@ -150,8 +150,7 @@ public void testProperties() throws Exception {
150150
channel.basicConsume(queueName, true, consumer);
151151
await("Message count > 0").until(() -> messageCount(rabbitAdmin, queueName) == 0);
152152
Properties props = rabbitAdmin.getQueueProperties(queueName);
153-
assertThat(props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT)).isNotNull();
154-
assertThat(props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT)).isEqualTo(1);
153+
assertThat(props).containsEntry(RabbitAdmin.QUEUE_CONSUMER_COUNT, 1);
155154
channel.close();
156155
}
157156
finally {
@@ -167,23 +166,23 @@ private long messageCount(RabbitAdmin rabbitAdmin, String queueName) {
167166
}
168167

169168
@Test
170-
public void testTemporaryLogs() throws Exception {
169+
public void testTemporaryLogs() {
171170
SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
172171
connectionFactory.setHost("localhost");
173172
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
174173
try {
175-
ApplicationContext ctx = mock(ApplicationContext.class);
176-
Map<String, Queue> queues = new HashMap<String, Queue>();
174+
ApplicationContext ctx = mock();
175+
Map<String, Queue> queues = new HashMap<>();
177176
queues.put("nonDurQ", new Queue("testq.nonDur", false, false, false));
178177
queues.put("adQ", new Queue("testq.ad", true, false, true));
179178
queues.put("exclQ", new Queue("testq.excl", true, true, false));
180179
queues.put("allQ", new Queue("testq.all", false, true, true));
181-
given(ctx.getBeansOfType(Queue.class)).willReturn(queues);
182-
Map<String, Exchange> exchanges = new HashMap<String, Exchange>();
180+
given(ctx.getBeansOfType(Queue.class, false, false)).willReturn(queues);
181+
Map<String, Exchange> exchanges = new HashMap<>();
183182
exchanges.put("nonDurEx", new DirectExchange("testex.nonDur", false, false));
184183
exchanges.put("adEx", new DirectExchange("testex.ad", true, true));
185184
exchanges.put("allEx", new DirectExchange("testex.all", false, true));
186-
given(ctx.getBeansOfType(Exchange.class)).willReturn(exchanges);
185+
given(ctx.getBeansOfType(Exchange.class, false, false)).willReturn(exchanges);
187186
rabbitAdmin.setApplicationContext(ctx);
188187
rabbitAdmin.afterPropertiesSet();
189188
Log logger = spy(TestUtils.getPropertyValue(rabbitAdmin, "logger", Log.class));
@@ -244,17 +243,17 @@ public void testMultiEntities() {
244243
assertThat(admin.getQueueProperties(ctx.getBean(Config.class).prototypeQueueName)).isNull();
245244
Declarables mixedDeclarables = ctx.getBean("ds", Declarables.class);
246245
assertThat(mixedDeclarables.getDeclarablesByType(Queue.class))
247-
.hasSize(1)
248-
.extracting(Queue::getName)
249-
.contains("q4");
246+
.hasSize(1)
247+
.extracting(Queue::getName)
248+
.contains("q4");
250249
assertThat(mixedDeclarables.getDeclarablesByType(Exchange.class))
251-
.hasSize(1)
252-
.extracting(Exchange::getName)
253-
.contains("e4");
250+
.hasSize(1)
251+
.extracting(Exchange::getName)
252+
.contains("e4");
254253
assertThat(mixedDeclarables.getDeclarablesByType(Binding.class))
255-
.hasSize(1)
256-
.extracting(Binding::getDestination)
257-
.contains("q4");
254+
.hasSize(1)
255+
.extracting(Binding::getDestination)
256+
.contains("q4");
258257
ctx.close();
259258
}
260259

@@ -274,14 +273,13 @@ public void testAvoidHangAMQP_508() {
274273

275274
@Test
276275
public void testIgnoreDeclarationExceptionsTimeout() throws Exception {
277-
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = mock(
278-
com.rabbitmq.client.ConnectionFactory.class);
276+
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = mock();
279277
TimeoutException toBeThrown = new TimeoutException("test");
280278
willThrow(toBeThrown).given(rabbitConnectionFactory).newConnection(any(ExecutorService.class), anyString());
281279
CachingConnectionFactory ccf = new CachingConnectionFactory(rabbitConnectionFactory);
282280
ccf.setExecutor(mock(ExecutorService.class));
283281
RabbitAdmin admin = new RabbitAdmin(ccf);
284-
List<DeclarationExceptionEvent> events = new ArrayList<DeclarationExceptionEvent>();
282+
List<DeclarationExceptionEvent> events = new ArrayList<>();
285283
admin.setApplicationEventPublisher(new EventPublisher(events));
286284
admin.setIgnoreDeclarationExceptions(true);
287285
admin.declareQueue(new AnonymousQueue());
@@ -304,13 +302,13 @@ public void testIgnoreDeclarationExceptionsTimeout() throws Exception {
304302

305303
@Test
306304
public void testWithinInvoke() throws Exception {
307-
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
308-
Connection connection = mock(Connection.class);
305+
ConnectionFactory connectionFactory = mock();
306+
Connection connection = mock();
309307
given(connectionFactory.createConnection()).willReturn(connection);
310-
Channel channel1 = mock(Channel.class);
311-
Channel channel2 = mock(Channel.class);
308+
Channel channel1 = mock();
309+
Channel channel2 = mock();
312310
given(connection.createChannel(false)).willReturn(channel1, channel2);
313-
DeclareOk declareOk = mock(DeclareOk.class);
311+
DeclareOk declareOk = mock();
314312
given(channel1.queueDeclare()).willReturn(declareOk);
315313
given(declareOk.getQueue()).willReturn("foo");
316314
RabbitTemplate template = new RabbitTemplate(connectionFactory);
@@ -331,14 +329,14 @@ public void testWithinInvoke() throws Exception {
331329
@Test
332330
@SuppressWarnings("removal")
333331
public void testRetry() throws Exception {
334-
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);
335-
com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class);
332+
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = mock();
333+
com.rabbitmq.client.Connection connection = mock();
336334
given(rabbitConnectionFactory.newConnection((ExecutorService) isNull(), anyString())).willReturn(connection);
337-
Channel channel = mock(Channel.class);
335+
Channel channel = mock();
338336
given(connection.createChannel()).willReturn(channel);
339337
given(channel.isOpen()).willReturn(true);
340338
willThrow(new RuntimeException()).given(channel)
341-
.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
339+
.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
342340
CachingConnectionFactory ccf = new CachingConnectionFactory(rabbitConnectionFactory);
343341
RabbitAdmin admin = new RabbitAdmin(ccf);
344342
RetryTemplate rtt = new RetryTemplate();
@@ -351,13 +349,13 @@ public void testRetry() throws Exception {
351349
ctx.getBeanFactory().initializeBean(admin, "admin");
352350
ctx.refresh();
353351
assertThatThrownBy(() -> ccf.createConnection())
354-
.isInstanceOf(UncategorizedAmqpException.class);
352+
.isInstanceOf(UncategorizedAmqpException.class);
355353
ctx.close();
356354
verify(channel, times(3)).queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any());
357355
}
358356

359357
@Test
360-
public void testLeaderLocator() throws Exception {
358+
public void testLeaderLocator() {
361359
CachingConnectionFactory cf = new CachingConnectionFactory(
362360
RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
363361
RabbitAdmin admin = new RabbitAdmin(cf);

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public void testNestedTxBinding() throws Exception {
444444
rabbitTemplate.setChannelTransacted(true);
445445
RabbitAdmin admin = new RabbitAdmin(rabbitTemplate);
446446
ApplicationContext ac = mock(ApplicationContext.class);
447-
willReturn(Collections.singletonMap("foo", new Queue("foo"))).given(ac).getBeansOfType(Queue.class);
447+
willReturn(Collections.singletonMap("foo", new Queue("foo"))).given(ac).getBeansOfType(Queue.class, false, false);
448448
admin.setApplicationContext(ac);
449449
admin.afterPropertiesSet();
450450
AtomicReference<Channel> templateChannel = new AtomicReference<>();

spring-rabbitmq-client/src/main/java/org/springframework/amqp/rabbitmq/client/RabbitAmqpAdmin.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,16 @@ private void declareDeclarableBeans() {
188188

189189
LOG.debug("Initializing declarations");
190190
Collection<Exchange> contextExchanges = new LinkedList<>(
191-
this.applicationContext.getBeansOfType(Exchange.class).values());
191+
this.applicationContext.getBeansOfType(Exchange.class, false, false).values());
192192
Collection<Queue> contextQueues = new LinkedList<>(
193-
this.applicationContext.getBeansOfType(Queue.class).values());
193+
this.applicationContext.getBeansOfType(Queue.class, false, false).values());
194194
Collection<Binding> contextBindings = new LinkedList<>(
195-
this.applicationContext.getBeansOfType(Binding.class).values());
195+
this.applicationContext.getBeansOfType(Binding.class, false, false).values());
196196
Collection<DeclarableCustomizer> customizers =
197-
this.applicationContext.getBeansOfType(DeclarableCustomizer.class).values();
197+
this.applicationContext.getBeansOfType(DeclarableCustomizer.class, false, false).values();
198198

199199
processDeclarables(contextExchanges, contextQueues, contextBindings,
200-
this.applicationContext.getBeansOfType(Declarables.class, false, true).values());
200+
this.applicationContext.getBeansOfType(Declarables.class, false, false).values());
201201

202202
final Collection<Exchange> exchanges = filterDeclarables(contextExchanges, customizers);
203203
final Collection<Queue> queues = filterDeclarables(contextQueues, customizers);

0 commit comments

Comments
 (0)