Java Interview Preparation
Dependency Injection and Beans Interview Questions
Dependency injection, bean wiring, scopes, lifecycle, and circular dependencies.
Dependency Injection and Beans Interview Focus
Dependency injection, bean wiring, scopes, lifecycle, and circular dependencies.
Interview questions
1. What is dependency injection? Beginner
Dependency injection is a design technique where an object receives the objects it needs instead of creating them itself. Spring’s container creates the dependencies and supplies them to the object.
2. What are the different types of dependency injection? Beginner
The common types are constructor injection, setter injection, and field injection. Constructor injection is normally preferred because required dependencies are explicit and the object can be immutable.
3. Why is constructor injection recommended? Intermediate
Constructor injection makes required dependencies visible, supports final fields, prevents partially initialized objects, and makes unit testing simple because dependencies can be passed directly.
4. What is the difference between constructor, setter, and field injection? Intermediate
Constructor injection is best for required dependencies. Setter injection suits optional or changeable dependencies. Field injection is short but hides dependencies and makes plain unit testing harder, so it is generally avoided.
5. What is autowiring? Beginner
Autowiring is Spring’s process of finding a bean that matches a dependency and injecting it automatically. Spring can match by type and use @Qualifier or @Primary when more than one candidate exists.
6. How does @Autowired work? Intermediate
@Autowired asks Spring to resolve and inject a dependency. Since Spring 4.3, a class with one constructor does not need @Autowired on that constructor; Spring uses it automatically.
7. Is @Autowired required for a single constructor? Beginner
@Autowired asks Spring to resolve and inject a dependency. Since Spring 4.3, a class with one constructor does not need @Autowired on that constructor; Spring uses it automatically.
8. What is the difference between @Qualifier and @Primary? Intermediate
@Primary marks the default bean when several candidates have the same type. @Qualifier selects a specific named or qualified bean at an injection point.
9. What happens when multiple beans of the same type exist? Intermediate
Spring reports an ambiguity error unless one bean is marked @Primary, the injection point uses @Qualifier, or the application injects a collection or map of all matching beans.
10. What are the different bean scopes? Beginner
Common scopes are singleton, prototype, request, session, application, and websocket. Singleton is the default, meaning one bean instance is created per ApplicationContext.
11. What is the default bean scope? Beginner
Common scopes are singleton, prototype, request, session, application, and websocket. Singleton is the default, meaning one bean instance is created per ApplicationContext.
12. What is the lifecycle of a Spring bean? Intermediate
Spring creates the bean, injects dependencies, applies post-processors, calls initialization callbacks, and makes it available. During context shutdown it calls destruction callbacks for beans that support them.
13. What are @PostConstruct and @PreDestroy? Intermediate
@PostConstruct runs after dependency injection and is useful for initialization. @PreDestroy runs before bean destruction and is useful for releasing resources.
14. What is a circular dependency? Intermediate
A circular dependency occurs when bean A needs bean B while bean B also needs bean A. Constructor cycles usually fail fast because neither object can be created first.
15. How do you resolve circular dependencies? Advanced
Prefer redesigning the classes so responsibilities are separated. If the relationship is genuinely optional, setter or lazy injection can help, but using it only to hide a design problem is not recommended.
How to Prepare for Java Technical Interviews
Use this Java technical interview question bank to revise core concepts and practise explaining your decisions clearly. The questions cover Java fundamentals, object-oriented programming, collections, exceptions, multithreading, Spring Boot, Hibernate, databases, and other topics used in real software development interviews.
Start with the fundamentals, then move to scenario-based questions and advanced topics. Filter questions by difficulty to build confidence gradually. A strong answer should define the concept, explain why it matters, and include a practical example when appropriate.
Continue your preparation with the Java tutorials, or explore all interview preparation tracks for managerial and company-focused questions.
