in wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java [225:296]
private String getBeanNameOfClass(final ApplicationContext ctx, final Class<?> clazz,
final ResolvableType resolvableType, String fieldName)
{
// get the list of all possible matching beans
List<String> names = new ArrayList<>(
Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(ctx, resolvableType)));
// filter out beans that are not candidates for autowiring
if (ctx instanceof AbstractApplicationContext)
{
Iterator<String> it = names.iterator();
while (it.hasNext())
{
final String possibility = it.next();
BeanDefinition beanDef = getBeanDefinition(
((AbstractApplicationContext)ctx).getBeanFactory(), possibility);
if (BeanFactoryUtils.isFactoryDereference(possibility) ||
possibility.startsWith("scopedTarget.") ||
(beanDef != null && !beanDef.isAutowireCandidate()))
{
it.remove();
}
}
}
if (names.size() > 1)
{
if (ctx instanceof AbstractApplicationContext)
{
List<String> primaries = new ArrayList<>();
for (String name : names)
{
BeanDefinition beanDef = getBeanDefinition(
((AbstractApplicationContext)ctx).getBeanFactory(), name);
if (beanDef instanceof AbstractBeanDefinition)
{
if (beanDef.isPrimary())
{
primaries.add(name);
}
}
}
if (primaries.size() == 1)
{
return primaries.get(0);
}
}
//use field name to find a match
int nameIndex = names.indexOf(fieldName);
if (nameIndex > -1)
{
return names.get(nameIndex);
}
StringBuilder msg = new StringBuilder();
msg.append("More than one bean of type [");
msg.append(clazz.getName());
msg.append("] found, you have to specify the name of the bean ");
msg.append("(@SpringBean(name=\"foo\")) or (@Named(\"foo\") if using @jakarta.inject classes) in order to resolve this conflict. ");
msg.append("Matched beans: ");
msg.append(Strings.join(",", names));
throw new IllegalStateException(msg.toString());
}
else if(!names.isEmpty())
{
return names.get(0);
}
return null;
}