Skip to content

Commit 5c68f3f

Browse files
committed
Reject @bean method with method-level @Autowired declaration
Closes gh-33051
1 parent a58e27e commit 5c68f3f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.beans.factory.parsing.Problem;
2223
import org.springframework.beans.factory.parsing.ProblemReporter;
2324
import org.springframework.core.type.MethodMetadata;
@@ -45,6 +46,12 @@ final class BeanMethod extends ConfigurationMethod {
4546
@Override
4647
@SuppressWarnings("NullAway")
4748
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+
4855
if ("void".equals(getMetadata().getReturnTypeName())) {
4956
// declared as void: potential misuse of @Bean, maybe meant as init method instead?
5057
problemReporter.error(new VoidDeclaredMethodError());
@@ -89,6 +96,15 @@ private static String getLocalMethodIdentifier(MethodMetadata metadata) {
8996
}
9097

9198

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+
92108
private class VoidDeclaredMethodError extends Problem {
93109

94110
VoidDeclaredMethodError() {

spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.ObjectFactory;
3131
import org.springframework.beans.factory.annotation.Autowired;
3232
import org.springframework.beans.factory.annotation.Value;
33+
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
3334
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3435
import org.springframework.beans.factory.support.RootBeanDefinition;
3536
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -47,6 +48,7 @@
4748
import org.springframework.util.Assert;
4849

4950
import static org.assertj.core.api.Assertions.assertThat;
51+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5052

5153
/**
5254
* System tests covering use of {@link Autowired} and {@link Value} within
@@ -187,10 +189,8 @@ void testValueInjectionWithProviderMethodArguments() {
187189

188190
@Test
189191
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));
194194
}
195195

196196
private void doTestValueInjection(BeanFactory context) {

0 commit comments

Comments
 (0)