Browse Source

Polishing

pull/31496/head
Juergen Hoeller 2 years ago
parent
commit
d254bff197
  1. 2
      framework-docs/modules/ROOT/pages/core/beans/annotation-config/autowired.adoc
  2. 53
      spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java

2
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 @@ -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

53
spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java

@ -17,6 +17,7 @@ @@ -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 @@ class AnnotationConfigApplicationContextTests { @@ -104,19 +105,19 @@ class AnnotationConfigApplicationContextTests {
// 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 @@ class AnnotationConfigApplicationContextTests { @@ -309,8 +310,8 @@ class AnnotationConfigApplicationContextTests {
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 @@ class AnnotationConfigApplicationContextTests { @@ -601,7 +602,6 @@ class AnnotationConfigApplicationContextTests {
BeanB b;
BeanC c;
@Autowired
BeanA(BeanB b, BeanC c) {
this.b = b;
@ -720,39 +720,18 @@ class AnnotationConfigApplicationContextTests { @@ -720,39 +720,18 @@ class AnnotationConfigApplicationContextTests {
}
}
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();
}
}

Loading…
Cancel
Save