Performance: @Component, @Service, @Repository, @Controller
These annotations affect application startup time and memory usage by controlling bean creation and scanning.
@Service public class UserService { ... } @Repository public class UserRepository { ... } @Controller public class UserController { ... }
@Component public class UserService { ... } @Component public class UserRepository { ... } @Component public class UserController { ... }
| Pattern | Bean Scanning | Startup Time | Memory Usage | Verdict |
|---|---|---|---|---|
| @Component everywhere | High - scans all classes as generic beans | Slower due to extra scanning | Higher due to many beans | [X] Bad |
| @Service, @Repository, @Controller used | Optimized - scans with layer context | Faster startup | Lower memory footprint | [OK] Good |
@Service
public class UserService {
public String getUser() {
return "Alice";
}
}
What is the role of this class in the application?@Repository
public class UserRepository {
public void saveUser(String user) {
// save logic
}
}
Options:
A) Class should be an interface extending JpaRepository
B) Missing @Service annotation
C) @Repository cannot be used on classes
D) Method saveUser must return a value@Component
public class UtilityHelper { }
@Service
public class OrderService { }
@Repository
public interface OrderRepository extends JpaRepository { }
@Controller
public class OrderController { }
Which statement correctly describes the roles and how Spring manages these beans?Pass the test and this pattern moves unknown