0
0
Spring Bootframework~10 mins

Bean concept in Spring 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 declare a Spring bean using annotation.

Spring Boot
@Configuration
public class AppConfig {
    @Bean
    public MyService [1]() {
        return new MyService();
    }
}
Drag options to blanks, or click blank then click option'
AcreateService
BserviceBean
CmyService
DgetService
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not follow Java naming conventions.
Forgetting to annotate the method with @Bean.
2fill in blank
medium

Complete the code to inject a bean into a Spring component using annotation.

Spring Boot
@Component
public class UserController {
    private final UserService userService;

    public UserController([1] UserService userService) {
        this.userService = userService;
    }
}
Drag options to blanks, or click blank then click option'
A@Autowired
B@Inject
C@Qualifier
D@Resource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Qualifier without specifying a bean name.
Forgetting to annotate the constructor parameter.
3fill in blank
hard

Fix the error in the bean scope declaration.

Spring Boot
@Bean
@Scope([1])
public SessionBean sessionBean() {
    return new SessionBean();
}
Drag options to blanks, or click blank then click option'
A"prototype"
B"session"
C"singleton"
D"request"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "singleton" scope for session-specific beans.
Forgetting to import the correct Scope annotation.
4fill in blank
hard

Fill both blanks to create a bean with a custom name and prototype scope.

Spring Boot
@Bean(name = "[1]")
@Scope("[2]")
public ProductService [3]() {
    return new ProductService();
}
Drag options to blanks, or click blank then click option'
AcustomProductService
Bsingleton
Cprototype
DproductService
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the bean name and method name.
Using singleton scope instead of prototype.
5fill in blank
hard

Fill all three blanks to define a bean with lazy initialization, singleton scope, and a qualifier.

Spring Boot
@Bean
@Lazy([1])
@Scope("[2]")
@Qualifier("[3]")
public NotificationService notificationService() {
    return new NotificationService();
}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Csingleton
DemailService
Attempts:
3 left
💡 Hint
Common Mistakes
Setting lazy to false when lazy initialization is desired.
Using incorrect qualifier names.