Reputation: 2915
I want to create bean when specific class type with specific name is missing, but ConditionalOnMissingBean
below does not work, since value and name are not related.
@ConditionalOnMissingBean(value=BeanName.class, name = "beanName")
Upvotes: 2
Views: 1937
Reputation: 1050
There is no such functionality of the box. However, you can easily implement it with custom Conditional:
class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String[] beanNames = context.getBeanFactory().getBeanNamesForType(BeanName.class);
return Stream.of(beanNames).anyMatch(beanName -> beanName.equals("beanName"));
}
}
and add it to your bean @Conditional(MyCondition.class)
Upvotes: 3