I have a Java Config class that looks similar to:
@Configuration
public class MyConfig {
@Autowired(required = true)
private MyRegistry registry;
public CustomAttributeConfig() {
. . .
}
@Bean
public IBeanApiType someBeanApi() {
return new SomeBeanApi();
}
@PostConstruct
public void postConstructionCodeHere() {
registry.register(someBeanApi());
}
. . .
Is the order of execution of the @Autowired
, @Bean
, and @PostConstruct
guaranteed in any way?
What if the @Bean
used the @Autowired
registry value?
The order of execution I am seeing in my real code is:
- the autowired field
- the @PostConstruct annotated method
- the @Bean annotated method
My goal is to have 2 and 3 to execute in reverse order. How do I know the order of mixtures of these annotations in a Java Config file and what options are available to control that order?
EDITTED! Thought about this some more and while I am interested in the order, that may not really be critical. I modified the code above slightly to reflect the fact that I want to call the @Bean constructor from the @PostConstruct code.
This seems to work fine for the very limited tests I have done. Does anyone know if all the necessary Spring initialization is guaranteed to be finished when a @PostConstruct is called such that calling an @Bean annotated method will always work? I did step into the calls and see the interceptors being invoked, but I don't know that the proxy I will get back will always be the "right" one, with all the advisors, advice, etc properly attached.