InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
SKM
Revision Mode
Document technical questions and best-practice answers.
Injecting One Class into Another
- If you have two classes, how would you inject one class into another using Spring Boot features?
To inject one class into another in Spring Boot, follow these simple steps:
1: Add @Component or @Service to the class you want to inject
@Component
public class ClassA {
// your code here
}
2: Use @Autowired in the class where you want to inject it
@Component
public class ClassB {
@Autowired
private ClassA classA; // ClassA is injected here
}
Summary:
1: Mark the class to be injected with @Component or @Service.
2: Use @Autowired in the other class to inject it automatically.
Ready for commit