Browse Source

Reject @Bean method with method-level @Autowired declaration

Closes gh-33051
pull/33065/head
Juergen Hoeller 2 years ago
parent
commit
5c68f3f4ef
  1. 16
      spring-context/src/main/java/org/springframework/context/annotation/BeanMethod.java
  2. 8
      spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

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

@ -18,6 +18,7 @@ package org.springframework.context.annotation; @@ -18,6 +18,7 @@ package org.springframework.context.annotation;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.core.type.MethodMetadata;
@ -45,6 +46,12 @@ final class BeanMethod extends ConfigurationMethod { @@ -45,6 +46,12 @@ final class BeanMethod extends ConfigurationMethod {
@Override
@SuppressWarnings("NullAway")
public void validate(ProblemReporter problemReporter) {
if (getMetadata().getAnnotationAttributes(Autowired.class.getName()) != null) {
// declared as @Autowired: semantic mismatch since @Bean method arguments are autowired
// in any case whereas @Autowired methods are setter-like methods on the containing class
problemReporter.error(new AutowiredDeclaredMethodError());
}
if ("void".equals(getMetadata().getReturnTypeName())) {
// declared as void: potential misuse of @Bean, maybe meant as init method instead?
problemReporter.error(new VoidDeclaredMethodError());
@ -89,6 +96,15 @@ final class BeanMethod extends ConfigurationMethod { @@ -89,6 +96,15 @@ final class BeanMethod extends ConfigurationMethod {
}
private class AutowiredDeclaredMethodError extends Problem {
AutowiredDeclaredMethodError() {
super("@Bean method '%s' must not be declared as autowired; remove the method-level @Autowired annotation."
.formatted(getMetadata().getMethodName()), getResourceLocation());
}
}
private class VoidDeclaredMethodError extends Problem {
VoidDeclaredMethodError() {

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

@ -30,6 +30,7 @@ import org.springframework.beans.factory.InitializingBean; @@ -30,6 +30,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@ -47,6 +48,7 @@ import org.springframework.core.io.Resource; @@ -47,6 +48,7 @@ import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* System tests covering use of {@link Autowired} and {@link Value} within
@ -187,10 +189,8 @@ class AutowiredConfigurationTests { @@ -187,10 +189,8 @@ class AutowiredConfigurationTests {
@Test
void testValueInjectionWithAccidentalAutowiredAnnotations() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ValueConfigWithAccidentalAutowiredAnnotations.class);
doTestValueInjection(context);
context.close();
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() ->
new AnnotationConfigApplicationContext(ValueConfigWithAccidentalAutowiredAnnotations.class));
}
private void doTestValueInjection(BeanFactory context) {

Loading…
Cancel
Save