I have a set of service classes that are spread across different maven modules in my application.
public class ServiceA implements IsService
{
public void startA()
{
....
}
}
public class ServiceB implements IsService
{
public void startB()
{
....
}
}
Creating a configuration class to detect and register only the classes that implement the IsService interface.
@Configuration
@ComponentScan(basePackages = {"com.subex.roc"} ,
useDefaultFilters = false ,
includeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE,value = IsService.class)})
public class ServiceRegisterConfig
{
}
Fetching the registered classes and calling start() polymorphically.
public void initServerServices()
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ServiceRegisterConfig.class);
context.refresh();
for (String beanDfn : context.getBeanDefinitionNames())
{
if (context.getBean(beanDfn) instanceof IsService) {
IsService service = (IsService) context.getBean(beanDfn);
service.start();
}
}
}
However I want to ensure that ServiceA 's start() is called first and then ServiceB as ServiceB depends on the initialization of ServiceA. Can the container somehow ensure this?
Tried DependsOn. But start() of services being called in the order of their encounter in classpath.DependsOn not being honoured.