Add String type attribute to OnBean conditions
Fixes gh-1381
This commit is contained in:
parent
a9bd720fb3
commit
e0c3dd79f9
|
@ -46,6 +46,13 @@ public @interface ConditionalOnBean {
|
|||
*/
|
||||
Class<?>[] value() default {};
|
||||
|
||||
/**
|
||||
* The class type names of bean that should be checked. The condition matches when any
|
||||
* of the classes specified is contained in the {@link ApplicationContext}.
|
||||
* @return the class type names of beans to check
|
||||
*/
|
||||
String[] type() default {};
|
||||
|
||||
/**
|
||||
* The annotation type decorating a bean that should be checked. The condition matches
|
||||
* when each class specified is missing from beans in the {@link ApplicationContext}.
|
||||
|
|
|
@ -46,6 +46,13 @@ public @interface ConditionalOnMissingBean {
|
|||
*/
|
||||
Class<?>[] value() default {};
|
||||
|
||||
/**
|
||||
* The class type names of bean that should be checked. The condition matches when
|
||||
* each class specified is missing in the {@link ApplicationContext}.
|
||||
* @return the class type names of beans to check
|
||||
*/
|
||||
String[] type() default {};
|
||||
|
||||
/**
|
||||
* The annotation type decorating a bean that should be checked. The condition matches
|
||||
* when each class specified is missing from all beans in the
|
||||
|
|
|
@ -290,6 +290,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
|
|||
.getAllAnnotationAttributes(annotationType.getName(), true);
|
||||
collect(attributes, "name", this.names);
|
||||
collect(attributes, "value", this.types);
|
||||
collect(attributes, "type", this.types);
|
||||
collect(attributes, "annotation", this.annotations);
|
||||
if (this.types.isEmpty() && this.names.isEmpty()) {
|
||||
addDeducedBeanType(context, metadata, this.types);
|
||||
|
|
|
@ -76,6 +76,14 @@ public class ConditionalOnBeanTests {
|
|||
assertEquals("bar", this.context.getBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassOnBeanClassNameCondition() {
|
||||
this.context.register(FooConfiguration.class, OnBeanClassNameConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertTrue(this.context.containsBean("bar"));
|
||||
assertEquals("bar", this.context.getBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnBeanConditionWithXml() {
|
||||
this.context.register(XmlConfiguration.class, OnBeanNameConfiguration.class);
|
||||
|
@ -100,6 +108,14 @@ public class ConditionalOnBeanTests {
|
|||
assertEquals("bar", this.context.getBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnMissingBeanType() throws Exception {
|
||||
this.context.register(FooConfiguration.class,
|
||||
OnBeanMissingClassConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertFalse(this.context.containsBean("bar"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(name = "foo")
|
||||
protected static class OnBeanNameConfiguration {
|
||||
|
@ -136,6 +152,24 @@ public class ConditionalOnBeanTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(type = "java.lang.String")
|
||||
protected static class OnBeanClassNameConfiguration {
|
||||
@Bean
|
||||
public String bar() {
|
||||
return "bar";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(type = "some.type.Missing")
|
||||
protected static class OnBeanMissingClassConfiguration {
|
||||
@Bean
|
||||
public String bar() {
|
||||
return "bar";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
protected static class FooConfiguration {
|
||||
|
|
Loading…
Reference in New Issue