Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 9, 2023
1 parent 6fc5a78 commit d254bff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -601,7 +602,6 @@ static class BeanA {
BeanB b;
BeanC c;


@Autowired
BeanA(BeanB b, BeanC c) {
this.b = b;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit d254bff

Please sign in to comment.