0
0
Spring Bootframework~10 mins

@Service annotation 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 service in Spring Boot.

Spring Boot
[1]
public class UserService {
    // service methods
}
Drag options to blanks, or click blank then click option'
A@Repository
B@Service
C@Controller
D@ComponentScan
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @Service
Forgetting to add any annotation
2fill in blank
medium

Complete the code to inject the service into a controller using Spring Boot.

Spring Boot
public class UserController {
    private final UserService userService;

    public UserController([1] userService) {
        this.userService = userService;
    }
}
Drag options to blanks, or click blank then click option'
AUserService
BInject
CService
D@Autowired
Attempts:
3 left
💡 Hint
Common Mistakes
Using annotation names as parameter type
Forgetting to use the service class type
3fill in blank
hard

Fix the error in the service class declaration by adding the missing annotation.

Spring Boot
[1]
public class OrderService {
    public void processOrder() {
        // processing logic
    }
}
Drag options to blanks, or click blank then click option'
A@Repository
B@Component
C@Controller
D@Service
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Service
Not adding any annotation
4fill in blank
hard

Fill both blanks to create a service class and inject it into a controller.

Spring Boot
@Service
public class ProductService {
    public String getProduct() {
        return "Product";
    }
}

public class ProductController {
    private final [1] productService;

    public ProductController([2] productService) {
        this.productService = productService;
    }
}
Drag options to blanks, or click blank then click option'
AProductService
BUserService
COrderService
DProductController
Attempts:
3 left
💡 Hint
Common Mistakes
Using controller class name instead of service
Mixing different service class names
5fill in blank
hard

Fill all three blanks to define a service, annotate it, and inject it into a controller.

Spring Boot
[1]
public class CustomerService {
    public String getCustomer() {
        return "Customer";
    }
}

public class CustomerController {
    private final [2] customerService;

    public CustomerController([3] customerService) {
        this.customerService = customerService;
    }
}
Drag options to blanks, or click blank then click option'
A@Service
BCustomerService
D@Controller
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @Service
Mismatching types in field and constructor