Currently the @DependsOn(...)
annotation is only configurable using String
, it would be nice to also support class type.
Example:
1- Current situation:
@Configuration
public class Config {
@Bean
public Bean1 bean1(){
return new Bean1();
}
@Bean
@DependsOn(value={"bean1"})
public Bean2 bean2(){
return new Bean2();
}
}
Enhancement:
@Configuration
public class Config {
@Bean
public Bean1 bean1(){
return new Bean1();
}
@Bean
@DependsOn(value={Bean1.class})
public Bean2 bean2(){
return new Bean2();
}
}
It's easier to remember the class than bean name. Thanks
Comment From: chenqimiao
@akuma8
The name of bean can specific the bean, but the class may represent more than one.
If @DependsOn
supports class attribute, it may seem confusing.
Comment From: akuma8
@chenqimiao I know, maybe throwing an exception in the case there are several beans of the same type or create those beans (of the same type) before creating the dependant bean.
Comment From: dingqianwen
I think it is a good choice to support @DependsOn (value={bean.class})
Comment From: snicoll
Thanks for the suggestion, but dependOns
is really meant to refer to a bean name as the metadata is required very early on in the bean factory lifecycle. Offering a class would mean that we'd be able to resolve the candidate bean types and we won't be able to do that reliably.