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 · Advanced · question 56 of 100

How do you implement and configure the Spring Boot Externalized Configuration to separate the application’s configuration from the code?

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

The Spring Boot Externalized Configuration feature allows developers to configure a Spring Boot application using external properties files instead of hardcoding the configuration in the code. This approach makes it easier to change the configuration of an application without having to recompile or redeploy it. It also enables developers to manage different configurations for different environments, such as development, testing, and production.

To implement the Externalized Configuration feature, Spring Boot supports several configuration file formats, including Properties, YAML, JSON, and XML. By default, Spring Boot looks for configuration files in the following locations:

Config file in the current directory

Config file in the classpath

Config file in the /config subdirectory of the current directory

Config file in the /config subdirectory of the classpath

Developers can also specify the location of the configuration file using the –spring.config.location command-line option or the SPRING_CONFIG_LOCATION environment variable.

To access the configuration properties in the code, developers can use the @Value annotation to inject the property value into a bean. For example, suppose the following property is defined in the application.properties file:

    myapp.message=Hello World

The property value can be injected into a bean using the @Value annotation:

    @Component
    public class MyBean {
        
        @Value("\${myapp.message}")
        private String message;
        
        // rest of the bean code
    }

Developers can also use the @ConfigurationProperties annotation to bind a group of related properties to a Java object. For example, suppose the following properties are defined in the application.properties file:

    myapp.user.name=John Doe
    myapp.user.email=johndoe@example.com

The properties can be bound to a User object using the @ConfigurationProperties annotation:

    @Component
    @ConfigurationProperties(prefix = "myapp.user")
    public class User {
        
        private String name;
        private String email;
        
        // getters and setters
    }

The User object can then be injected into other beans and used in the code.

Overall, the Spring Boot Externalized Configuration feature provides a flexible and convenient way to configure a Spring Boot application using external properties files, making it easier to manage configuration and deploy the application to different environments.

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