diff --git a/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc b/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc index 595675cc2959..02e6a3d2ceae 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc @@ -364,8 +364,6 @@ ignored if it cannot be autowired. This allows properties to be assigned default that can be optionally overridden via dependency injection. ==== - - [[beans-autowired-annotation-constructor-resolution]] Injected constructor and factory method arguments are a special case since the `required` attribute in `@Autowired` has a somewhat different meaning due to Spring's constructor diff --git a/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java index 115226e1b3bd..1eb25d791ec5 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java @@ -17,6 +17,7 @@ package org.springframework.context.annotation; import java.util.Map; +import java.util.Objects; import java.util.regex.Pattern; import org.junit.jupiter.api.Test; @@ -104,19 +105,19 @@ void getBeanByTypeRaisesNoSuchBeanDefinitionException() { // attempt to retrieve a bean that does not exist Class targetType = Pattern.class; - assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> - context.getBean(targetType)) - .withMessageContaining(format("No qualifying bean of type '%s'", targetType.getName())); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> context.getBean(targetType)) + .withMessageContaining(format("No qualifying bean of type '%s'", targetType.getName())); } @Test void getBeanByTypeAmbiguityRaisesException() { ApplicationContext context = new AnnotationConfigApplicationContext(TwoTestBeanConfig.class); - assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> - context.getBean(TestBean.class)) - .withMessageContaining("No qualifying bean of type '" + TestBean.class.getName() + "'") - .withMessageContaining("tb1") - .withMessageContaining("tb2"); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> context.getBean(TestBean.class)) + .withMessageContaining("No qualifying bean of type '" + TestBean.class.getName() + "'") + .withMessageContaining("tb1") + .withMessageContaining("tb2"); } /** @@ -309,8 +310,8 @@ void individualBeanWithNullReturningSupplier() { assertThat(context.getBeansOfType(BeanB.class).values().iterator().next()).isSameAs(context.getBean(BeanB.class)); assertThat(context.getBeansOfType(BeanC.class).values().iterator().next()).isSameAs(context.getBean(BeanC.class)); - assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> - context.getBeanFactory().resolveNamedBean(BeanA.class)); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> context.getBeanFactory().resolveNamedBean(BeanA.class)); assertThat(context.getBeanFactory().resolveNamedBean(BeanB.class).getBeanInstance()).isSameAs(context.getBean(BeanB.class)); assertThat(context.getBeanFactory().resolveNamedBean(BeanC.class).getBeanInstance()).isSameAs(context.getBean(BeanC.class)); } @@ -601,7 +602,6 @@ static class BeanA { BeanB b; BeanC c; - @Autowired BeanA(BeanB b, BeanC c) { this.b = b; @@ -720,39 +720,18 @@ static class FactoryBeanInjectionPoints extends FactoryResultInjectionPoint { } } + class TestBean { String name; @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (name == null ? 0 : name.hashCode()); - return result; + public boolean equals(@Nullable Object other) { + return (this == other || (other instanceof TestBean that && Objects.equals(this.name, that.name))); } @Override - public boolean equals(@Nullable Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - TestBean other = (TestBean) obj; - if (name == null) { - if (other.name != null) { - return false; - } - } - else if (!name.equals(other.name)) { - return false; - } - return true; + public int hashCode() { + return this.name.hashCode(); } - }