0
0
Spring Bootframework~10 mins

@Configuration and @Bean 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 the class as a configuration class.

Spring Boot
@[1]
public class AppConfig {
}
Drag options to blanks, or click blank then click option'
AService
BComponent
CConfiguration
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Configuration
Forgetting the '@' symbol
Using unrelated annotations like @Service
2fill in blank
medium

Complete the code to define a bean method that returns a new instance of MyService.

Spring Boot
@Bean
public MyService [1]() {
    return new MyService();
}
Drag options to blanks, or click blank then click option'
AcreateService
BmyService
CserviceBean
DgetService
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid method names
Forgetting to add @Bean annotation
Returning null instead of new instance
3fill in blank
hard

Fix the error in the bean method declaration by completing the missing annotation.

Spring Boot
@[1]
public MyRepository [2]() {
    return new MyRepository();
}
Drag options to blanks, or click blank then click option'
A@Repository
B@Service
CmyRepository
D@Bean
Attempts:
3 left
💡 Hint
Common Mistakes
Using class-level annotations on methods
Omitting the @Bean annotation
Using @Component on methods instead of classes
4fill in blank
hard

Fill both blanks to complete the configuration class with a bean method.

Spring Boot
@[1]
public class ServiceConfig {
    @[2]
    public UserService userService() {
        return new UserService();
    }
}
Drag options to blanks, or click blank then click option'
AConfiguration
BComponent
CBean
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up class and method annotations
Using @Component instead of @Configuration on class
Forgetting @Bean on method
5fill in blank
hard

Fill all three blanks to create a configuration class with two bean methods.

Spring Boot
@[1]
public class AppBeans {
    @[2]
    public ServiceA serviceA() {
        return new ServiceA();
    }

    @[3]
    public ServiceB serviceB() {
        return new ServiceB();
    }
}
Drag options to blanks, or click blank then click option'
AConfiguration
BBean
CComponent
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component on class instead of @Configuration
Forgetting @Bean on one or both methods
Using @Service on methods