tgoop.com/topJavaQuizExplain/307
Last Update:
What will be the output of the code?
✅ A. Initializing Helper Ready Cleaning
❌ B. Initializing Helper Ready HelperService: Cleaning Cleaning
❌ C. Helper Ready Initializing Helper Ready Cleaning
❌ D. Compilation Error: Missing @Component annotation on MyService
❌ E. None of the above
Explanation:
1. Configuration and Bean Lifecycle:
• AppConfig is annotated with Configuration, marking it as the configuration class.
• Bean(initMethod = "init", destroyMethod = "cleanup") defines the lifecycle methods for MyService.
2. Dependency Injection:
• MyService has a Autowired field, injecting the HelperService bean. Spring automatically resolves the dependency.
3. Lifecycle Methods:
• The init method of MyService is called after the bean is instantiated.
• The cleanup method is called when the context is closed.
4. Output Order:
• Initializing is printed when init() is invoked.
• The process() method prints the message from HelperService: Helper Ready.
• Cleaning is printed during context closure.
Correct answer: A
BY Explanations “Top Java Quiz Questions”
Share with your friend now:
tgoop.com/topJavaQuizExplain/307