0
0
Spring Bootframework~10 mins

Conditional bean creation 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 create a bean only if a property is set to true.

Spring Boot
@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "[1]")
public Feature feature() {
    return new Feature();
}
Drag options to blanks, or click blank then click option'
Afalse
Benabled
Cyes
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' instead of 'true' disables the bean creation.
Using arbitrary strings like 'yes' or 'enabled' won't match the property value.
2fill in blank
medium

Complete the code to create a bean only if another bean named 'dataSource' exists.

Spring Boot
@Bean
@ConditionalOnBean(name = "[1]")
public Repository repository() {
    return new Repository();
}
Drag options to blanks, or click blank then click option'
Aservice
BdataSource
CcacheManager
DentityManager
Attempts:
3 left
💡 Hint
Common Mistakes
Using a bean name that does not exist causes the bean not to be created.
Confusing bean names like 'entityManager' or 'cacheManager' when the condition is on 'dataSource'.
3fill in blank
hard

Fix the error in the code to create a bean only if a class 'com.example.Service' is present.

Spring Boot
@Bean
@ConditionalOnClass(name = "[1]")
public Service service() {
    return new Service();
}
Drag options to blanks, or click blank then click option'
Acom.example.Service
Bcom.example.services.Service
Cexample.Service
Dcom.example.service
Attempts:
3 left
💡 Hint
Common Mistakes
Using package name only without class name.
Incorrect capitalization or missing parts of the package.
4fill in blank
hard

Fill both blanks to create a bean only if property 'app.mode' equals 'prod' and a bean named 'cacheManager' exists.

Spring Boot
@Bean
@ConditionalOnProperty(name = "app.mode", havingValue = "[1]")
@ConditionalOnBean(name = "[2]")
public CacheService cacheService() {
    return new CacheService();
}
Drag options to blanks, or click blank then click option'
Aprod
Bdev
CcacheManager
DdataSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dev' instead of 'prod' disables the bean creation.
Checking for a bean named 'dataSource' instead of 'cacheManager'.
5fill in blank
hard

Fill all three blanks to create a bean only if class 'com.example.Feature' is present, property 'feature.enabled' is 'true', and bean 'featureRepository' exists.

Spring Boot
@Bean
@ConditionalOnClass(name = "[1]")
@ConditionalOnProperty(name = "feature.enabled", havingValue = "[2]")
@ConditionalOnBean(name = "[3]")
public Feature feature() {
    return new Feature();
}
Drag options to blanks, or click blank then click option'
Acom.example.Feature
Btrue
CfeatureRepository
Dcom.example.Features
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the class name or package.
Using 'false' instead of 'true' for the property.
Checking for a bean with a wrong name.