0
0
Spring Bootframework~10 mins

@ConfigurationProperties for type-safe config in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable binding of properties to the class.

Spring Boot
@[1]("app.settings")
public class AppSettings {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Drag options to blanks, or click blank then click option'
AComponent
BConfigurationProperties
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @ConfigurationProperties
Forgetting to specify the prefix in the annotation
2fill in blank
medium

Complete the code to register the configuration properties class as a Spring bean.

Spring Boot
@Bean
public [1] appSettings() {
    return new AppSettings();
}
Drag options to blanks, or click blank then click option'
AAppSettings
BComponent
CConfigurationProperties
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the annotation name instead of the class
Returning a Spring stereotype annotation instead of the class
3fill in blank
hard

Fix the error in the class to properly bind nested properties.

Spring Boot
public class DatabaseProperties {
    private String url;
    private Credentials [1];

    public static class Credentials {
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Credentials getCredentials() {
        return credentials;
    }

    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
    }
}
Drag options to blanks, or click blank then click option'
Acredentials
Bcredential
CCredentials
DcredentialInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase for the field name
Using a different name than the getter/setter methods
4fill in blank
hard

Fill both blanks to complete the annotation and enable validation.

Spring Boot
@[1]("server.config")
@[2]
public class ServerConfig {
    @NotNull
    private Integer port;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}
Drag options to blanks, or click blank then click option'
AConfigurationProperties
BValidated
CComponent
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting @Validated annotation
Using @Component instead of @ConfigurationProperties
5fill in blank
hard

Fill all three blanks to create a type-safe configuration with nested properties and validation.

Spring Boot
@[1]("mail.settings")
@[2]
public class MailSettings {
    private Smtp smtp;

    public static class Smtp {
        @NotEmpty
        private String host;

        @Min([3])
        private int port;

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }
    }

    public Smtp getSmtp() {
        return smtp;
    }

    public void setSmtp(Smtp smtp) {
        this.smtp = smtp;
    }
}
Drag options to blanks, or click blank then click option'
AConfigurationProperties
BValidated
C1
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting @Validated annotation
Setting @Min to 0 or negative values
Using @Component instead of @ConfigurationProperties