Spring Framework Interview Questions
This section covers a wide range of Spring Framework interview questions, suitable for various experience levels.
1. What is Spring?
Spring is a lightweight, open-source application framework for Java. It simplifies enterprise application development by providing a cohesive programming and configuration model.
2. Advantages of the Spring Framework.
- Provides pre-built templates and components.
- Promotes loose coupling.
- Simplifies testing.
- Lightweight design.
- Faster development.
- Supports powerful abstraction mechanisms.
- Offers declarative programming capabilities.
3. Modules of the Spring Framework.
(This section would list Spring modules like Spring Core, Spring AOP, Spring Data Access/Integration, Spring Web, and more.)
4. IOC (Inversion of Control) and DI (Dependency Injection).
Inversion of Control (IOC) and Dependency Injection (DI) are design patterns that promote loose coupling. Instead of creating dependencies within a class, dependencies are provided (injected) from the outside, making the code more modular, testable, and maintainable.
Example: Without DI
public class Employee {
Address address = new Address(); // Tight coupling
}
Example: With DI
public class Employee {
Address address;
Employee(Address address) {
this.address = address;
}
}
5. Role of the IOC Container.
The Spring IOC container manages the creation, configuration, and assembly of application objects (beans).
6. Types of IOC Containers.
BeanFactory
: A basic container.ApplicationContext
: An advanced container extendingBeanFactory
.
7. BeanFactory
vs. ApplicationContext
.
ApplicationContext
is a more feature-rich container than BeanFactory
, providing additional services like AOP support and internationalization.
8. Constructor Injection vs. Setter Injection.
Injection Type | Description |
---|---|
Constructor Injection | Dependencies are provided through the constructor. All dependencies must be provided at object creation time; no partial injection. |
Setter Injection | Dependencies are set using setter methods after the object is created. Allows partial injection (setting some but not all dependencies). |
9. Autowiring in Spring.
Autowiring automatically resolves and injects dependencies into beans based on specified modes (by name, by type, constructor). This eliminates explicit dependency injection configuration in many cases.
10. Bean Scopes.
Scope | Description |
---|---|
singleton |
One instance per Spring container (default). |
prototype |
A new instance each time it is requested. |
request |
One instance per HTTP request. |
session |
One instance per HTTP session. |
globalsession |
One instance per global HTTP session (portlet context only). |
11. Singleton vs. Prototype Scope.
singleton
is typically used for stateless beans; prototype
is used for stateful beans.
12. Spring Transaction Management.
- Programmatic: Managing transactions directly in code.
- Declarative: Managing transactions using XML or annotations.
Spring JDBC Interview Questions
13. Advantages of JdbcTemplate
.
JdbcTemplate
simplifies JDBC operations, reducing boilerplate code and improving efficiency.
14. Spring JDBC API Classes.
JdbcTemplate
SimpleJdbcTemplate
NamedParameterJdbcTemplate
SimpleJdbcInsert
SimpleJdbcCall
15. Fetching Records with JdbcTemplate
.
Use query()
with ResultSetExtractor
or RowMapper
.
16. Advantage of NamedParameterJdbcTemplate
.
Uses named parameters, making SQL queries more readable and maintainable than using question marks (?
).
17. Advantage of SimpleJdbcTemplate
.
Supports varargs and autoboxing.
Spring AOP (Aspect-Oriented Programming) Interview Questions
18. What is AOP?
AOP (Aspect-Oriented Programming) is a programming paradigm separating cross-cutting concerns (like logging or security) from core business logic. This modularizes the code and makes it easier to maintain.
19. Advantages of Spring AOP.
AOP improves modularity, making code cleaner and easier to maintain. It enables adding functionality (aspects) without modifying core code.
20. AOP Terminology.
(This section would define AOP terms like JoinPoint, Advice, Pointcut, Aspect, etc.)
21. What is a Join Point?
A join point represents a point in your program's execution where an aspect can be applied (e.g., method execution, exception handling).
22. Does Spring AOP Support All Join Points?
No, Spring AOP primarily supports method execution join points.
23. What is Advice?
Advice is the action taken by an aspect at a join point (e.g., logging a message before a method is called).
24. Types of Advice.
- Before advice
- After advice
- After returning advice
- After throwing advice
- Around advice
25. What is a Pointcut?
A pointcut is an expression defining where advice should be applied (which join points).
26. What is an Aspect?
An aspect combines advice and pointcuts to define a cross-cutting concern.
27. What is Introduction?
Introduction adds new methods or fields to an existing class.
28. What is a Target Object?
The object to which an aspect is applied.
29. What is an Interceptor?
An interceptor is similar to an aspect but typically contains only a single piece of advice.
30. What is Weaving?
Weaving is the process of linking aspects with other parts of the application.
31. Compile-Time Weaving in Spring AOP.
No, Spring AOP performs weaving at runtime.
32. AOP Implementations.
- Spring AOP
- AspectJ
- Other AOP frameworks
Spring MVC (Model-View-Controller) Interview Questions
33. Front Controller in Spring MVC.
DispatcherServlet
.
34. @Controller
Annotation.
Marks a class as a Spring MVC controller.
35. @RequestMapping
Annotation.
Maps HTTP requests to controller methods.
36. ViewResolver
Class.
Resolves views (JSPs, etc.) to be rendered.
37. Commonly Used ViewResolver
.
InternalResourceViewResolver
.
38. Validation Support in Spring MVC.
Yes, Spring MVC provides validation support (often using annotations like @Valid
).