Skip to content

Commit 24a8f1b

Browse files
quaffjhoeller
authored andcommitted
AnnotatedBeanDefinitionReader should respect @fallback qualifier analogous to @primary
Also add tests to cover qualifier classes.
1 parent 159e237 commit 24a8f1b

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* @author Chris Beams
4444
* @author Sam Brannen
4545
* @author Phillip Webb
46+
* @author Yanming Zhou
4647
* @since 3.0
4748
* @see AnnotationConfigApplicationContext#register
4849
*/
@@ -266,6 +267,9 @@ private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name,
266267
if (Primary.class == qualifier) {
267268
abd.setPrimary(true);
268269
}
270+
else if (Fallback.class == qualifier) {
271+
abd.setFallback(true);
272+
}
269273
else if (Lazy.class == qualifier) {
270274
abd.setLazyInit(true);
271275
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2024 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.context.annotation;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.beans.factory.annotation.Qualifier;
27+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
28+
import org.springframework.context.support.GenericApplicationContext;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* @author Yanming Zhou
34+
*/
35+
class AnnotatedBeanDefinitionReaderTests {
36+
37+
@Test
38+
void registerBeanWithQualifiers() {
39+
GenericApplicationContext context = new GenericApplicationContext();
40+
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
41+
42+
reader.registerBean(TestBean.class, "primary", Primary.class);
43+
assertThat(context.getBeanDefinition("primary").isPrimary()).isTrue();
44+
45+
reader.registerBean(TestBean.class, "fallback", Fallback.class);
46+
assertThat(context.getBeanDefinition("fallback").isFallback()).isTrue();
47+
48+
reader.registerBean(TestBean.class, "lazy", Lazy.class);
49+
assertThat(context.getBeanDefinition("lazy").isLazyInit()).isTrue();
50+
51+
reader.registerBean(TestBean.class, "customQualifier", CustomQualifier.class);
52+
assertThat(context.getBeanDefinition("customQualifier"))
53+
.isInstanceOfSatisfying(AbstractBeanDefinition.class, abd ->
54+
assertThat(abd.hasQualifier(CustomQualifier.class.getTypeName())).isTrue());
55+
}
56+
57+
@Lazy(false)
58+
static class TestBean {
59+
60+
}
61+
62+
@Target({ElementType.TYPE, ElementType.METHOD})
63+
@Retention(RetentionPolicy.RUNTIME)
64+
@Qualifier
65+
@interface CustomQualifier {
66+
67+
}
68+
}

0 commit comments

Comments
 (0)