Skip to content

Commit 6628bcb

Browse files
garyrussellartembilan
authored andcommitted
GH-1370: Containers: Find Admin in Parent Context
Resolves #1370
1 parent 35b233e commit 6628bcb

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/AbstractMessageListenerContainer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.springframework.aop.framework.ProxyFactory;
7272
import org.springframework.aop.support.DefaultPointcutAdvisor;
7373
import org.springframework.beans.BeansException;
74+
import org.springframework.beans.factory.BeanFactoryUtils;
7475
import org.springframework.beans.factory.BeanNameAware;
7576
import org.springframework.beans.factory.DisposableBean;
7677
import org.springframework.context.ApplicationContext;
@@ -1802,8 +1803,10 @@ protected void updateLastReceive() {
18021803
}
18031804

18041805
protected void configureAdminIfNeeded() {
1805-
if (this.amqpAdmin == null && this.getApplicationContext() != null) {
1806-
Map<String, AmqpAdmin> admins = this.getApplicationContext().getBeansOfType(AmqpAdmin.class);
1806+
if (this.amqpAdmin == null && this.applicationContext != null) {
1807+
Map<String, AmqpAdmin> admins =
1808+
BeanFactoryUtils.beansOfTypeIncludingAncestors(this.applicationContext, AmqpAdmin.class,
1809+
false, false);
18071810
if (admins.size() == 1) {
18081811
this.amqpAdmin = admins.values().iterator().next();
18091812
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.amqp.rabbit.listener;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
24+
import org.springframework.amqp.rabbit.core.RabbitAdmin;
25+
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
26+
import org.springframework.amqp.rabbit.junit.RabbitAvailableCondition;
27+
import org.springframework.amqp.utils.test.TestUtils;
28+
import org.springframework.context.support.GenericApplicationContext;
29+
30+
/**
31+
* @author Gary Russell
32+
* @since 2.4
33+
*
34+
*/
35+
@RabbitAvailable
36+
public class ContainerAdminTests {
37+
38+
@Test
39+
void findAdminInParentContext() {
40+
GenericApplicationContext parent = new GenericApplicationContext();
41+
CachingConnectionFactory cf =
42+
new CachingConnectionFactory(RabbitAvailableCondition.getBrokerRunning().getConnectionFactory());
43+
RabbitAdmin admin = new RabbitAdmin(cf);
44+
parent.registerBean(RabbitAdmin.class, () -> admin);
45+
parent.refresh();
46+
GenericApplicationContext child = new GenericApplicationContext(parent);
47+
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
48+
child.registerBean(SimpleMessageListenerContainer.class, () -> container);
49+
child.refresh();
50+
container.start();
51+
assertThat(TestUtils.getPropertyValue(container, "amqpAdmin")).isSameAs(admin);
52+
container.stop();
53+
}
54+
55+
}

0 commit comments

Comments
 (0)