Skip to content

Commit a2e29d3

Browse files
committed
Fix issues 28298, findAnnotationOnBean finds annotations from a static @bean method's enclosing class
fix import style
1 parent c9e7816 commit a2e29d3

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
747747
// Check raw bean class, e.g. in case of a proxy.
748748
if (bd.hasBeanClass()) {
749749
Class<?> beanClass = bd.getBeanClass();
750-
if (beanClass != beanType) {
750+
751+
// If the bean definitation has factory method , then ignore it's configuration class's annotations
752+
if (beanClass != beanType && bd.getResolvedFactoryMethod() == null) {
751753
MergedAnnotation<A> annotation =
752754
MergedAnnotations.from(beanClass, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
753755
if (annotation.isPresent()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2002-2022 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.beans.factory;
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.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for @Bean annotation lookup.
34+
*
35+
* @author tao zhang
36+
*/
37+
class DefaultListableBeanFactoryAnnotationLookupTests {
38+
39+
@Test
40+
void beanDefinedInInstanceMethodDoesNotHaveAnnotationsFromItsConfigurationClass() {
41+
beanDoesNotHaveAnnotationsFromItsConfigurationClass(InstanceBeanMethodConfiguration.class);
42+
}
43+
44+
@Test
45+
void beanDefinedInStaticMethodDoesNotHaveAnnotationsFromItsConfigurationClass() {
46+
beanDoesNotHaveAnnotationsFromItsConfigurationClass(StaticBeanMethodConfiguration.class);
47+
}
48+
49+
void beanDoesNotHaveAnnotationsFromItsConfigurationClass(Class<?> config) {
50+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(config)) {
51+
ExampleAnnotation annotation = context.getBeanFactory().findAnnotationOnBean("exampleBean",
52+
ExampleAnnotation.class);
53+
assertThat(annotation).isNull();
54+
}
55+
}
56+
57+
@Target(ElementType.TYPE)
58+
@Retention(RetentionPolicy.RUNTIME)
59+
static @interface ExampleAnnotation {
60+
61+
}
62+
63+
@Configuration
64+
@ExampleAnnotation
65+
static class StaticBeanMethodConfiguration {
66+
67+
@Bean
68+
static String exampleBean() {
69+
return "example";
70+
}
71+
72+
}
73+
74+
@Configuration
75+
@ExampleAnnotation
76+
static class InstanceBeanMethodConfiguration {
77+
78+
@Bean
79+
String exampleBean() {
80+
return "example";
81+
}
82+
83+
}
84+
85+
}

0 commit comments

Comments
 (0)