Spring Boot Resources

Spring Boot - Configuration/Auto-Configuration



This chapter will discuss in detail about Spring Boot configuration and auto-configuration.

Spring Boot Configuration

Although a Spring/Spring Boot application can be configured in an XML document, it is generally recommended to configure Spring Boot applications with a single file with @Configuration annotation.

Different kinds of annotation

@Component Annotation

In Spring Boot, the @Component annotation declares a class as a Spring component. This signals to Spring that the class should be managed by the Spring container, meaning it will be automatically created, initialized, and potentially injected into other components.

Example - Usage of @Component Annotation

import org.springframework.stereotype.Component;

@Component
public class Utility {
...
}

@Configuration Annotation

In Spring Boot, the @Configuration annotation is used to indicate that a class is a source of bean definitions. It's a core annotation from the Spring Framework, not specific to Spring Boot. Classes marked with @Configuration can define beans using the @Bean annotation on methods within the class. These beans are then managed by the Spring container. @Configuration promotes a Java-based configuration approach instead of relying solely on XML configuration files.

Example - Usage of @Configuration Annotation

@Configuration
public class AppConfig {

   @Bean
   public MyService myService() {
      return new MyServiceImpl();
   }
}

@SpringBootApplication Annotation

This is a meta-annotation that combines @EnableAutoConfiguration, @ComponentScan, and @Configuration. It's often the only annotation you need to bootstrap a Spring Boot application.

Example - Usage of @SpringBootApplication Annotation

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class MyApplication {

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

@ComponentScan Annotation

In Spring Boot, the @ComponentScan annotation is used to instruct Spring to scan the specified packages and their sub-packages for components (classes annotated with @Component, @Service, @Repository, @Controller, etc.) and register them as beans in the Spring application context. By default, if you don't explicitly use @ComponentScan, Spring Boot automatically scans the package where your main application class resides and its sub-packages. If your components are in a different package structure, you can use basePackages attribute of @ComponentScan to specify the packages to scan. You can specify multiple packages using an array in basePackages, e.g.,

basePackages = {"com.example.package1", "com.example.package2"}

Example - Usage of @ComponentScan Annotation

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.mypackage"}) 
public class MyApplication {

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

What is Spring Boot Autoconfiguration?

Spring Boot Autoconfiguration is a feature that automatically configures your Spring application based on the dependencies present on the classpath and various properties settings. This eliminates the need for a lot of manual configuration, making Spring Boot applications easier to set up and maintain.

@SpringBootApplication or @EnableAutoConfiguration Annotation

These annotations trigger the autoconfiguration process. Typically, you annotate your main application class with @SpringBootApplication, which includes @EnableAutoConfiguration. Additional packages can be configured using the @AutoConfigurationPackage annotation.

  • Scanning the Classpath − Spring Boot scans the classpath for libraries and frameworks.

  • Conditional Configuration − Based on the detected dependencies, Spring Boot applies relevant configurations. This is done using @Conditional annotations, such as:

    • @ConditionalOnClass− Applies configuration if a specific class is present on the classpath.

    • @ConditionalOnMissingBean− Applies configuration if a specific bean is not already defined.

    • @ConditionalOnProperty− Applies configuration based on property values.

  • Bean Creation and Configuration − Spring Boot creates and configures beans based on the applied autoconfigurations.

    Example:

    • If you add the spring-boot-starter-web dependency to your project, Spring Boot automatically configures a Tomcat web server and other necessary components for a web application.

    • If you add the spring-boot-starter-data-jpa dependency, Spring Boot automatically configures a data source, entity manager, and other components required for JPA-based data access.

Benefits of Spring Boot Autoconfiguration

Following are some of the major benefits of using Spring Boot Autoconfiguration.

  • Reduced Boilerplate Code− Autoconfiguration eliminates the need for manual configuration of common Spring components.

  • Faster Development− You can quickly create Spring Boot applications without having to write extensive configuration code.

  • Improved Productivity− Autoconfiguration allows you to focus on the core business logic of your application.

  • Customization− You can customize the autoconfiguration behavior by following means:

    • Excluding specific autoconfiguration classes using the exclude attribute of @EnableAutoConfiguration.

    • Defining your own beans to override the auto-configured ones.

    • Using property files or environment variables to modify the default configurations.

Advertisements