Skip to content

Commit

Permalink
Reject @bean method with method-level @Autowired declaration
Browse files Browse the repository at this point in the history
Closes gh-33051
  • Loading branch information
jhoeller committed Jun 17, 2024
1 parent a58e27e commit 5c68f3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -89,6 +96,15 @@ private static String getLocalMethodIdentifier(MethodMetadata metadata) {
}


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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand All @@ -47,6 +48,7 @@
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
Expand Down Expand Up @@ -187,10 +189,8 @@ void testValueInjectionWithProviderMethodArguments() {

@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) {
Expand Down

0 comments on commit 5c68f3f

Please sign in to comment.