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 · Intermediate · question 23 of 100

How can you create and use custom configuration properties in a Spring Boot application?

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

Custom configuration properties are a useful feature of Spring Boot that allow developers to define and use custom properties in their applications. These properties can be used to configure various aspects of the application, such as database connections, email settings, and other settings that are specific to the application.

To create and use custom configuration properties in a Spring Boot application, you can follow these steps:

Define the custom properties: Define the custom properties as fields in a configuration class using the @ConfigurationProperties annotation. Each property should have a unique name and a default value.

    @Configuration
    @ConfigurationProperties(prefix = "myapp")
    public class MyAppProperties {
        private String name = "MyApp";
        private String version = "1.0.0";
        
        // getters and setters
    }

Configure the prefix: Use the prefix attribute of the @ConfigurationProperties annotation to specify a prefix for the custom properties. This prefix will be used to group the custom properties together.

Enable the configuration: Use the @EnableConfigurationProperties annotation to enable the configuration class.

    @SpringBootApplication
    @EnableConfigurationProperties(MyAppProperties.class)
    public class MyApplication {
        // application code
    }

Use the custom properties: Use the custom properties in the application code by autowiring the configuration class and accessing the properties using their getters.

    @RestController
    public class MyController {
        @Autowired
        private MyAppProperties myAppProperties;
        
        @GetMapping("/")
        public String hello() {
            return "Hello from " + myAppProperties.getName() + " version " + myAppProperties.getVersion();
        }
    }

In this example, we have defined a custom configuration class MyAppProperties that contains two properties name and version. We have used the @ConfigurationProperties annotation to define the prefix for the properties and the @EnableConfigurationProperties annotation to enable the configuration class. In the MyController class, we have autowired the MyAppProperties class and used the properties in the hello() method.

Overall, custom configuration properties are a useful feature of Spring Boot that can help simplify the process of configuring applications. By defining custom properties and using them in the application code, developers can reduce the amount of boilerplate code that is required and make it easier to configure and maintain the application over time.

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