0
0
Spring Bootframework~10 mins

Custom auto-configuration 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 mark a class as a Spring Boot auto-configuration.

Spring Boot
@[1]
public class MyAutoConfiguration {
}
Drag options to blanks, or click blank then click option'
AEnableAutoConfiguration
BComponent
CConfiguration
DSpringBootApplication
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Configuration
Using @EnableAutoConfiguration on the class directly
2fill in blank
medium

Complete the code to conditionally enable the auto-configuration only if a bean of type DataSource is missing.

Spring Boot
@ConditionalOnMissingBean([1].class)
public class MyAutoConfiguration {
}
Drag options to blanks, or click blank then click option'
ADataSource
BRestTemplate
CJdbcTemplate
DEntityManager
Attempts:
3 left
💡 Hint
Common Mistakes
Using JdbcTemplate instead of DataSource
Using EntityManager which is JPA specific
3fill in blank
hard

Fix the error in the code to properly register the auto-configuration class in Spring Boot.

Spring Boot
META-INF/spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=[1]
Drag options to blanks, or click blank then click option'
Acom.example.MyAutoConfiguration.class
Bcom.example.MyAutoConfiguration.java
Corg.springframework.boot.autoconfigure.EnableAutoConfiguration
Dcom.example.MyAutoConfiguration
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '.class' suffix
Using the annotation name instead of class name
4fill in blank
hard

Fill both blanks to create a bean method that returns a RestTemplate only if no other RestTemplate bean exists.

Spring Boot
@Bean
@ConditionalOnMissingBean([1].class)
public [2] restTemplate() {
    return new RestTemplate();
}
Drag options to blanks, or click blank then click option'
ARestTemplate
BDataSource
DJdbcTemplate
Attempts:
3 left
💡 Hint
Common Mistakes
Using DataSource in condition but returning RestTemplate
Mismatching return type and condition class
5fill in blank
hard

Fill all three blanks to create a conditional auto-configuration that loads only if property 'app.feature.enabled' is true and a bean of type FeatureService is missing.

Spring Boot
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "[1]")
@ConditionalOnMissingBean([2].class)
@Configuration
public class FeatureAutoConfiguration {
    @Bean
    public [3] featureService() {
        return new FeatureService();
    }
}
Drag options to blanks, or click blank then click option'
Atrue
BFeatureService
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' as property value
Mismatching bean type in condition and method return