I liked annotations used for bean declaration etc. But now we have so many beans with order (@Depends). It is tough to maintain or look at a glance the configuration.
Is there any tool that provides "Effective Spring Config" information based on all your bean annotations?
Answer: you should not be using that many @DependsOn
annotations.
From the javadoc:
Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.
You can just do this:
@Configuration
public class MyConfig {
@Bean
public MovieClient movieClient(RestOperations rest) {
return new MovieClientImpl(rest);
}
@Bean
public RestOperations restOps() {
return new RestTemplate();
}
}
In this example, the RestOperations
bean will be instantiaded before the MovieClient
bean, just because the movieClient bean asks for it in the constructor. You don't need any @DependsOn
annotion in cases like this one.
Edit: as OP commented, there is still the issue of showing the "Effective Spring Config".
I do not think there is any tool for that, because your dependencies may change at runtime (because of AutoConfiguration, @Conditional
annotations, profiles, other?).
If you need to know your "Effective Spring Config" (i.e. "what beans are present at runtime"), you can do this:
ConfigurableApplicationContest context;
context = SpringApplication.run(Application.class, finalArgs);
// Print all the beans:
System.out.println(context.getBeanDefinitionNames());
However, if you meant how can you view and navigate all the configuration, you can organize your beans in different @Configuration
files, pick them up using @ComponentScan
, and navigate them using the Spring IDE pluguin for Eclipse, like this:
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