I use Spring Boot MVC application.
I have a @Configuration class that initializes a bean into ApplicationContext using @Bean.
I have a @Controller class into which I am trying to autowire the bean using @Autowired annotation.
Result: @Autowired field is null.
DEBUG: I tried to debug to see the order of execution. I was expecting to see that class annotated with @Configuration will run first to initialize bean into application context. However, controller class got instantiated first. Then @Bean method of configuration class got called next. Due to this bean is instantiated after controller and that is why Controller is not getting bean autowired.
Question: How to have @Configuration @Bean method execute prior to the controller class instantiation?
code for configuration class:
@Configuration
public class RootConfiguration2 {
@Autowired
private IService<ActBinding> bindingService;
@Bean
public Map<String, String> getBingindNameToRoutingKeyMap() throws Exception {
ListOperation<ActBinding> operation = ListOperation.from("key", "name", "exchangeId");
operation.sort("key", SortOrder.Ascending);
Iterable<ActBinding> result = bindingService.list(operation).getResult();
Map<String, String> bindingNameToRoutingKey = new HashMap<>();
result.forEach(x -> bindingNameToRoutingKey.put(x.getName(), x.getKey()));
return Collections.unmodifiableMap(bindingNameToRoutingKey);
}
}