Search code examples
springspring-boot

How spring boot scan and configures bean


I am new to spring boot. I have very basic question about component scanning by spring boot. follwoing is my main class.

package com.xyz;
@SpringBootApplication
public class MyApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(MyApplication.class, args);
    }
}

This is how i am running the application.

java -classpath /home/user/sprintboot-hello-world/target/lib/* com.xyz.MyApplication

As i understand when SpringApplication.run method is called, it scans and configures all the beans. some of the beans it configures are in third party library jar.
by reading the documents i understand it will scan all the classes in the main class package and its sub package because it is annotated with @SpringBootApplication.
But i do not understand how it configures the beans which are in different package residing in the third party library jar.
Does it scans all the classes in all the packages in the classpath and look for the presence of bean and configures it?


Solution

  • That is the Spring Boot Auto Configuration that handles loading of third party beans. When creating a library, there is a meta file that will be included along with it that will tell Spring where to load from. You can find more info on it here https://docs.spring.io/spring-boot/reference/features/developing-auto-configuration.html#features.developing-auto-configuration.locating-auto-configuration-candidates. So if you look into any of the Spring Boot Starter Jars, you will see this meta file with the location of the jars Auto-Config.