Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to get "Effective Spring Config" from all spring beans annotations?

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?

like image 718
gpa Avatar asked Aug 31 '25 01:08

gpa


1 Answers

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:

Navigate Spring Configuration

like image 172
ESala Avatar answered Sep 02 '25 14:09

ESala