Use of @SpringBootApplication

Spring Boot applications should have an entry point class with the public static void main(String[] args) method, which is usually annotated with the @SpringBootApplication annotation which is used to bootstrap the application

It indicates a configuration class that declares one or more methods which triggers auto-configuration and component scanning. This class is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

Fig : @SpringBootApplication

In this diagram, we can say that the @SpringBootApplication annotation has composed functionality from three annotations—@EnableAutoConfiguration, @ComponentScan,and @Configuration.

You can use these annotations to follow the convention over configuration pattern, making your apps easier to develop and maintain, and without worrying too much about its configuration.

Lets see the below code :

    package com.mycompany.myproject;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class Application {

        public static void main(Spring[] args) {
            SpringApplication.run(Application.class, args);
        }
    }                                                
                                                

Here are the meanings of these annotations:

  • @EnableAutoConfiguration :

    Spring Boot provides the @EnableAutoConfiguration annotation that is responsible for enabling the auto-configuration feature. This annotation is used in the main application file of the Spring Boot application. when this annotation is used on a Spring Java configuration class it causes Spring Boot to automatically create beans it thinks you need, usually based on classpath contents.

    Let's see the following code that represents the main application launcher class in the Spring Boot application:

        @Configuration
        @EnableAutoConfiguration
        public class Application {
        
            public static void main(Spring[] args) {
                SpringApplication.run(Application.class, args);
            }
        }                        
                                                                

    It is highly recommended that you put the main entry point class in the root package, say in com.mycompany.myproject, so that the @EnableAutoConfiguration and @ComponentScan annotations will scan for Spring beans, JPA entities, etc., in the root and all of its sub-packages automatically.

    If you have an entry point class in a nested package, you might need to specify the basePackages to scan for Spring components explicitly.

  • @Auto-configuration :

    Auto-configuration is one of the important features in Spring Boot because it configures your Spring Boot application according to your classpath, annotations, and any other configuration declarations, such as JavaConfig classes or XML.

    NOTE - Spring Boot Starter reduces a build's dependencies and Spring Boot auto-configuration reduces the Spring configuration.

  • @ComponentScan :

    The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

    The basePackageClasses attribute is a type-safe alternative to basePackages. When you specify basePackageClasses, Spring will scan the package (and subpackages) of the classes you specify.

    Basic code example :

        @ComponentScans({@ComponentScan("com.example1"), @ComponentScan("com.example1")})
        public class DemoAutoConfiguration {
        
        }
                                                            

    some of the useful attributes of @ComponentScan:

    • basePackages - can be used to state specific packages to scan.
    • useDefaultFilters - by setting this attribute to false (defaults true) you can make sure spring does not scan @Component, @Repository, @Service, or @Controller automatically.
    • includeFilters - can be used to include specific spring annotations / regex patterns to include in package scanning.
    • excludeFilters - can be used to exclude specific spring annotations / regex patterns to include in package scanning.