How Spring-Boot autocofiguration works?
“How Spring-Boot autocofiguration works?” This is a common question you will face in interview for java, microservice interiew.
Here is some tips …
How spring-boot will autoconfigure works ?
Spring-boot engine will scan your classpath and try to build required beans/configuration based on given configuration/properties.
example: If you have added spring-boot-starter-data-jpa dependecy in your pom or gradle, spring assumes that you want to use some databse persistent mechanism. It will choose hibernate as your default ORM and try to build configuration class fr you.
If required prperties not found that spring will throw and exception.
How spring knows how to construct a bean for given configuration?
Once you create a project boot will download necesary dependency for project.
The important(for our question) library/jar here is spring-boot-autoconfigure-.jar, here it came with default configuration classes which helps boot to intialize beans for given configuration.
example: you can see below image, i am using spring boot version
1.5.3.RELEASE so it downloads
spring-boot-autoconfigure-1.5.3.RELEASE.jar jar contains so many packages and classes for configuration, i have pointed out data > jpa which helps boot to initilize beans.
This annotation will enable spring’s autoconfiguraion feature to scna classpath and build a beans for you.
in boot application it is wrapped under
You will find this annotation in many classes of
spring-boot-autoconfigure-.jar, this annotation helps to decide wheather to register a bean or not.
There are some other annotations are used in auto configuration like
@ConditionalOnProperty, you can read official API Documentation for more detail.
At last spring already provided classes which are used to build a beans based on given configuration and by the help of some annotation for conditions, you have to provide custom configuration for framework which you want to use is not supported by boot yet.
Hope, this will help you to answer interview question.