Top interview questions
1. Design Patterns Used in Spring Framework
Spring extensively uses various design patterns. Key ones:
| Pattern | Where Used in Spring | Explanation |
|---|---|---|
| Dependency Injection (DI) | Spring IoC Container | Injects dependencies instead of creating them internally. |
| Singleton Pattern | Spring Bean default scope | Spring beans are singleton by default. |
| Factory Method Pattern | BeanFactory, ApplicationContext | Creates bean instances. |
| Prototype Pattern | Prototype-scoped beans | Returns new bean instance every time. |
| Proxy Pattern | AOP, Transaction Management | Creates proxy objects for cross-cutting concerns. |
| Template Method Pattern | JdbcTemplate, RestTemplate | Defines skeleton of process and allows custom logic. |
| Observer Pattern | Spring Events (ApplicationEventPublisher) | Event listeners for communication. |
| Front Controller Pattern | DispatcherServlet in Spring MVC | Single entry point for HTTP requests. |
| Builder Pattern | Bean definition & configurations | Creating complex bean definitions. |
2. Spring Bean Lifecycle
Spring bean lifecycle steps:
-
Instantiate — Object creation
-
Populate Properties — Dependency Injection
-
BeanNameAware / BeanFactoryAware / ApplicationContextAware (if implemented)
-
BeanPostProcessor —
postProcessBeforeInitialization() -
InitializingBean —
afterPropertiesSet()OR custom init method -
BeanPostProcessor —
postProcessAfterInitialization() -
Bean is Ready to Use
-
Destroy Phase
-
DisposableBean.destroy()OR custom destroy method
3. Difference Between Singleton and Static Class
| Feature | Singleton | Static Class |
|---|---|---|
| Instance | Single object instance | No object, directly accessed |
| Memory use | Created once lazily or eagerly | Loaded once in memory at JVM start |
| Implementation | Design pattern | Language feature |
| Inheritance | Can implement interfaces, inherit | Cannot extend classes or implement interfaces |
| Use case | Shared state, resources | Utility or helper methods |
4. Multiple Inheritance in Java
-
Java does not support multiple inheritance with classes
-
To avoid diamond problem
-
-
Java supports multiple inheritance through interfaces
Example:
Comments
Post a Comment