Complete the code to create a bean only if a property is set to true.
@Bean @ConditionalOnProperty(name = "feature.enabled", havingValue = "[1]") public Feature feature() { return new Feature(); }
The @ConditionalOnProperty annotation checks if the property feature.enabled has the value true to create the bean.
Complete the code to create a bean only if another bean named 'dataSource' exists.
@Bean @ConditionalOnBean(name = "[1]") public Repository repository() { return new Repository(); }
The @ConditionalOnBean annotation creates the bean only if a bean named dataSource exists in the context.
Fix the error in the code to create a bean only if a class 'com.example.Service' is present.
@Bean @ConditionalOnClass(name = "[1]") public Service service() { return new Service(); }
The @ConditionalOnClass annotation requires the full class name including package and class name exactly as com.example.Service.
Fill both blanks to create a bean only if property 'app.mode' equals 'prod' and a bean named 'cacheManager' exists.
@Bean @ConditionalOnProperty(name = "app.mode", havingValue = "[1]") @ConditionalOnBean(name = "[2]") public CacheService cacheService() { return new CacheService(); }
The bean cacheService is created only if the property app.mode is 'prod' and the bean named cacheManager exists.
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.
@Bean @ConditionalOnClass(name = "[1]") @ConditionalOnProperty(name = "feature.enabled", havingValue = "[2]") @ConditionalOnBean(name = "[3]") public Feature feature() { return new Feature(); }
The bean feature is created only if the class com.example.Feature is present, the property feature.enabled is 'true', and the bean named featureRepository exists.