I need one Component to be initialized before another. With @DependsOn it would look something like this:
@Component("beana")
public class BeanA{
@PostConstruct
void init(){
// do smth
}
}
@Component("beanb")
@DependsOn("beana")
public class BeanB{
@PostConstruct
void init(){
// do smth
}
}
I now have to tell BeanB that it depends on the initialization of BeanA. My problem is that I do not want BeanB to know about BeanAs existance (e.g, when BeanB is just publishing Events in an EventBus while initializing and BeanA handles these events). I would like to use an annotation at BeanA stating it should bei initialized before BeanB. So it would something like this:
@Component("beana")
@RequiredBy("beanb")
public class BeanA{
@PostConstruct
void init(){
// do smth
}
}
@Component("beanb")
public class BeanB{
@PostConstruct
void init(){
// do smth
}
}
Is there any Spring annotation or possibility to handle it like this?
I believe there is no out of the box spring annotation for this, but you can easily make your own one.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RequiredBy {
String[] value();
}
Then it is possible to iterate through all bean definitions and set dependsOn to that required
bean.
@Component
public static class RequiredByBeanDefinitionPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
for (String beanName : registry.getBeanDefinitionNames()) {
final BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
if (beanDefinition.getBeanClassName() == null) {
continue;
}
try {
final Class<?> beanClass = Class.forName(beanDefinition.getBeanClassName());
if (beanClass.isAnnotationPresent(RequiredBy.class)) {
final String[] dependantBeanNames = beanClass.getAnnotation(RequiredBy.class).value();
for (String dependantBeanName : dependantBeanNames) {
BeanDefinition dependantBeanDefinition = registry.getBeanDefinition(dependantBeanName);
dependantBeanDefinition.setDependsOn(beanName);
}
}
}
catch (ClassNotFoundException e) { throw new RuntimeException(e); }
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { }
}
And then use it like in you example:
@Component("beanA")
public static class BeanA {
@PostConstruct
private void init() {
System.out.println(this.getClass().getSimpleName());
}
}
@Component("beanB")
@RequiredBy({ "beanC", "beanA" })
public static class BeanB {
@PostConstruct
private void init() {
System.out.println(this.getClass().getSimpleName());
}
}
@Component("beanC")
@RequiredBy("beanA")
public static class BeanC {
@PostConstruct
private void init() {
System.out.println(this.getClass().getSimpleName());
}
}
=>
BeanB
BeanC
BeanA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With