WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Basic · question 10 of 100

What is auto-configuration in Spring Boot?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

Auto-configuration is a powerful feature of Spring Boot that aims to reduce the amount of configuration needed to get a Spring Boot application up and running. It achieves this by automatically configuring various beans and components based on the presence of certain dependencies and the runtime environment.

Here are some key aspects of auto-configuration in Spring Boot:

Conditional bean registration: Auto-configuration works by conditionally registering beans based on the presence of certain dependencies or properties in the runtime environment. For example, if the H2 database driver is detected on the classpath, Spring Boot will automatically configure a DataSource bean for the H2 database.

Classpath scanning: Auto-configuration also involves scanning the classpath for certain annotations and classes, and automatically configuring beans based on those annotations and classes. For example, if a @Controller annotation is detected on a class, Spring Boot will automatically configure a bean for that controller.

Default properties: Auto-configuration also provides default values for various properties and settings, which can be overridden by the application’s configuration files. For example, if no value is provided for the server.port property, Spring Boot will automatically configure the server to listen on port 8080.

Here is an example of how auto-configuration works in Spring Boot:

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

In the example above, the @SpringBootApplication annotation is used to indicate that this is a Spring Boot application. This annotation includes several other annotations, including @Configuration, @EnableAutoConfiguration, and @ComponentScan. The @EnableAutoConfiguration annotation is what enables auto-configuration in the application.

Here is an example of how auto-configuration can be customized in a Spring Boot application:

    @Configuration
    public class MyConfig {
        @Bean
        public DataSource dataSource() {
            // ...
        }
    }

In the example above, we have created a configuration class called MyConfig that defines a custom DataSource bean. This bean will be used instead of the auto-configured DataSource bean, since it has a higher priority.

Overall, auto-configuration is a powerful and flexible feature of Spring Boot that can greatly simplify the process of building and deploying Spring Boot applications. By automatically configuring various beans and components, Spring Boot reduces the amount of boilerplate code and configuration that is required, and allows developers to focus on building the core functionality of their applications.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics