File tree 2 files changed +20
-4
lines changed
main/java/org/springframework/context/annotation
test/java/org/springframework/context/annotation/configuration
2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change 18
18
19
19
import java .util .Map ;
20
20
21
+ import org .springframework .beans .factory .annotation .Autowired ;
21
22
import org .springframework .beans .factory .parsing .Problem ;
22
23
import org .springframework .beans .factory .parsing .ProblemReporter ;
23
24
import org .springframework .core .type .MethodMetadata ;
@@ -45,6 +46,12 @@ final class BeanMethod extends ConfigurationMethod {
45
46
@ Override
46
47
@ SuppressWarnings ("NullAway" )
47
48
public void validate (ProblemReporter problemReporter ) {
49
+ if (getMetadata ().getAnnotationAttributes (Autowired .class .getName ()) != null ) {
50
+ // declared as @Autowired: semantic mismatch since @Bean method arguments are autowired
51
+ // in any case whereas @Autowired methods are setter-like methods on the containing class
52
+ problemReporter .error (new AutowiredDeclaredMethodError ());
53
+ }
54
+
48
55
if ("void" .equals (getMetadata ().getReturnTypeName ())) {
49
56
// declared as void: potential misuse of @Bean, maybe meant as init method instead?
50
57
problemReporter .error (new VoidDeclaredMethodError ());
@@ -89,6 +96,15 @@ private static String getLocalMethodIdentifier(MethodMetadata metadata) {
89
96
}
90
97
91
98
99
+ private class AutowiredDeclaredMethodError extends Problem {
100
+
101
+ AutowiredDeclaredMethodError () {
102
+ super ("@Bean method '%s' must not be declared as autowired; remove the method-level @Autowired annotation."
103
+ .formatted (getMetadata ().getMethodName ()), getResourceLocation ());
104
+ }
105
+ }
106
+
107
+
92
108
private class VoidDeclaredMethodError extends Problem {
93
109
94
110
VoidDeclaredMethodError () {
Original file line number Diff line number Diff line change 30
30
import org .springframework .beans .factory .ObjectFactory ;
31
31
import org .springframework .beans .factory .annotation .Autowired ;
32
32
import org .springframework .beans .factory .annotation .Value ;
33
+ import org .springframework .beans .factory .parsing .BeanDefinitionParsingException ;
33
34
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
34
35
import org .springframework .beans .factory .support .RootBeanDefinition ;
35
36
import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
47
48
import org .springframework .util .Assert ;
48
49
49
50
import static org .assertj .core .api .Assertions .assertThat ;
51
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
50
52
51
53
/**
52
54
* System tests covering use of {@link Autowired} and {@link Value} within
@@ -187,10 +189,8 @@ void testValueInjectionWithProviderMethodArguments() {
187
189
188
190
@ Test
189
191
void testValueInjectionWithAccidentalAutowiredAnnotations () {
190
- AnnotationConfigApplicationContext context =
191
- new AnnotationConfigApplicationContext (ValueConfigWithAccidentalAutowiredAnnotations .class );
192
- doTestValueInjection (context );
193
- context .close ();
192
+ assertThatExceptionOfType (BeanDefinitionParsingException .class ).isThrownBy (() ->
193
+ new AnnotationConfigApplicationContext (ValueConfigWithAccidentalAutowiredAnnotations .class ));
194
194
}
195
195
196
196
private void doTestValueInjection (BeanFactory context ) {
You can’t perform that action at this time.
0 commit comments