Show List

Spring Boot auto-configuration

Spring Boot auto-configuration is a feature that automatically configures a Spring application based on the dependencies present in the classpath. This can greatly simplify the process of setting up a Spring application and can help you get started quickly.

For example, if you have a spring-boot-starter-web dependency in your classpath, Spring Boot will automatically configure a web application with a embedded Tomcat server. Similarly, if you have a spring-boot-starter-jdbc dependency, Spring Boot will automatically configure a JDBC DataSource.

Here's an example of how you can use Spring Boot auto-configuration to configure a simple web application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class MyApplication {

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

  @GetMapping("/")
  public String home() {
    return "Hello, World!";
  }
}

In this example, we use the @SpringBootApplication annotation to enable Spring Boot auto-configuration. The @RestController annotation is used to create a simple REST endpoint.

With these two annotations, Spring Boot will automatically configure a web application with a embedded Tomcat server, and you can start the application by running the main method.

You can access the endpoint by navigating to http://localhost:8080/ in a web browser, and you should see the message "Hello, World!" displayed.

This is just a simple example, but Spring Boot auto-configuration can be used to configure many other components and features, such as databases, security, caching, and more.


    Leave a Comment


  • captcha text