Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

Need clarification about the recommended way of bootstrapping a Spring Boot application

In the Java Docs for SpringApplication, it is mentioned that:

SpringApplications can read beans from a variety of different sources. It is generally recommended that a single @Configuration class is used to bootstrap your application [...]

Does this statement mean that it is recommended for an app to only have one @Configuration class, or is it saying that it is recommended to pass a class that has been annotated with @Configuration to SpringApplication (e.g. to its run method) -- without putting any limitations on the number of @Configuration classes?

Also I find the rest of the statement difficult to interpret:

[...] however, any of the following sources can also be used:

  • Class - A Java class to be loaded by AnnotatedBeanDefinitionReader
  • Resource - An XML resource to be loaded by XmlBeanDefinitionReader, or a groovy script to be loaded by GroovyBeanDefinitionReader
  • Package - A Java package to be scanned by ClassPathBeanDefinitionScanner
  • CharSequence - A class name, resource handle or package name to loaded as appropriate. If the CharSequence cannot be resolved to class and does not resolve to a Resource that exists it will be considered a Package.

While I have used SpringApplication.run before without any problems, now reading its JavaDocs description has me totally confused.

Upvotes: 0

Views: 295

Answers (2)

This is how I understand it :

a single @Configuration class is used to bootstrap your application

You should have one @Configuration class that is the root configuration of your application.

This makes the config much easier to understand and to debug. For sure you can use @Import to import other configurations.

In reality you may have different root configrations for tests and for prod. But you will use always only one as the starting point.

however, any of the following sources can also be used:

That part describes which type of sources can be used to create the application context. These sources can be passed to the constructor :

public SpringApplication(Object... sources)

or a setter :

public void setSources(Set<Object> sources)
  • Class - a class annotated with @Configuration
  • Resource - a xml file with spring configuration
  • Package - a package that will be scanned to find configurations
  • CharSequence - a string with the name of a class or package. This allows e.g. to use configuration classes, that are not available at compile time.

I personally have never configured a SpringApplication providing the sources to the constructor or setter. I have one root configuration and annotate my SpringApplication class with @Import.

Upvotes: 1

Ahmad Abdulaziz
Ahmad Abdulaziz

Reputation: 87

When you using @SpringApplication or @EnableAutoConfiguration spring boot will automatically configure your Spring application based on the jar dependencies that you have added

and it should be used just once in your application , On another hand @Configuration it could be used as many as needed in same root where @SpringApplication and @EnableAutoConfiguration.

Spring recommended to have just One primary @Configuration that add @EnableAutoConfiguration

you can include and exclude as many as you need @Configurations to the Primary @EnableAutoConfiguration

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html#using-boot-auto-configuration

from my understanding @EnableAutoConfiguration and @SpringApplication almost have same job

Upvotes: 0

Related Questions