Spring Interview Questions and Answers


What is the Spring Framework?
  • Spring is a Java-based framework used to build enterprise-level applications. It provides infrastructure support for developing Java applications.
What are the main features of Spring?
  • Lightweight, Inversion of Control (IoC), Aspect-Oriented Programming (AOP), Transaction Management, MVC Framework, Security, and Integration.
What is Inversion of Control (IoC)?
  • IoC is a design principle where the control of object creation and lifecycle is given to the framework/container rather than the application code.
What is Dependency Injection (DI)?
  • DI is a technique to achieve IoC, where the dependencies of objects are injected at runtime by the container.
What are the types of DI in Spring?
  • Constructor Injection and Setter Injection.
What is a Spring Bean?
  • A Spring Bean is an object managed by the Spring IoC container, created, configured, and wired by Spring.
What is the default scope of a Spring Bean?
  • The default scope is Singleton.
What are different Bean scopes in Spring?
  • Singleton, Prototype, Request, Session, Application, and Websocket (for web-aware applications).
What is the difference between BeanFactory and ApplicationContext?
  • BeanFactory is a basic container, while ApplicationContext is an advanced container with more features like internationalization, event propagation, and AOP.
What are the different ways to configure Spring?
  • XML-based configuration, Annotation-based configuration, and Java-based configuration.
What is @Component annotation?
  • @Component is used to mark a class as a Spring Bean for component scanning.
What is the difference between @Component, @Service, @Repository, and @Controller?
  • All are stereotypes: @Component is generic, @Service is for business logic, @Repository is for DAO layer, and @Controller is for web layer.
What is Spring Boot?
  • Spring Boot is a project that simplifies Spring application development by providing default configurations and embedded servers.
What is @SpringBootApplication?
  • @SpringBootApplication is a combination of @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations.
What is auto-configuration in Spring Boot?
  • Auto-configuration automatically configures your Spring application based on the dependencies in the classpath.
What is Spring Data JPA?
  • Spring Data JPA is a part of Spring Data project that simplifies JPA-based data access using repository interfaces.
What is a Spring Repository?
  • A repository is an interface that manages data access. It's part of the DAO layer and can extend JpaRepository, CrudRepository, etc.
What is @Autowired?
  • @Autowired is used to inject dependencies automatically by Spring’s dependency injection.
What is @Qualifier?
  • @Qualifier is used along with @Autowired to avoid confusion when multiple beans of the same type exist.
What is @Primary?
  • @Primary is used to give preference to one bean when multiple candidates are present for autowiring.
What is Spring AOP?
  • Spring AOP provides aspect-oriented programming support, allowing separation of cross-cutting concerns like logging and security.
What are the different types of advice in Spring AOP?
  • Before, After, AfterReturning, AfterThrowing, and Around advice.
What is a pointcut in Spring AOP?
  • A pointcut defines at which join points advice should be applied.
What is a join point in Spring AOP?
  • A join point is a point in the execution of the program such as method execution, where advice can be applied.
What is a proxy in Spring?
  • A proxy is an object created by Spring AOP to apply advice methods around the actual target object.
What is Spring MVC?
  • Spring MVC is a module of Spring for building web applications using the Model-View-Controller design pattern.
What is DispatcherServlet?
  • DispatcherServlet is the front controller in Spring MVC that routes HTTP requests to appropriate controllers.
What is @Controller in Spring?
  • @Controller is used to define a controller class in Spring MVC to handle web requests.
What is @RestController?
  • @RestController is a convenience annotation that combines @Controller and @ResponseBody to build RESTful APIs.
What is @RequestMapping?
  • @RequestMapping is used to map HTTP requests to handler methods of MVC and REST controllers.
What is @PathVariable?
  • @PathVariable is used to extract values from URI template and bind them to method parameters.
What is @RequestParam?
  • @RequestParam is used to extract query parameters from the request URL and bind them to method arguments.
What is @RequestBody?
  • @RequestBody binds the HTTP request body to a method parameter in a controller.
What is @ResponseBody?
  • @ResponseBody converts the return value of a method to the HTTP response body automatically.
What is Spring Security?
  • Spring Security is a framework that provides authentication, authorization, and protection against common security vulnerabilities.
What is CSRF in Spring Security?
  • CSRF (Cross Site Request Forgery) is an attack that tricks users into executing unwanted actions. Spring Security provides built-in protection against it.
What is Spring Transaction Management?
  • Spring provides a consistent programming model for transaction management, both programmatic and declarative using @Transactional.
What is @Transactional?
  • @Transactional is used to manage transaction boundaries declaratively.
What is the difference between checked and unchecked exceptions in Spring transactions?
  • By default, Spring rolls back transactions for unchecked exceptions only. You can configure it to roll back for checked exceptions as well.
How does Spring Boot handle configuration?
  • Using application.properties or application.yml files to configure application settings easily.
What is the difference between application.properties and application.yml?
  • Both are used for configuration. YAML is more readable and hierarchical, while properties files are simpler.
What is Spring Actuator?
  • Spring Boot Actuator provides production-ready features like health checks, metrics, info, and environment details via HTTP endpoints.
What is Spring DevTools?
  • DevTools provides developer productivity features such as automatic restarts and live reload for Spring Boot apps.
How do you handle exceptions in Spring Boot?
  • Using @ControllerAdvice and @ExceptionHandler annotations.
What is the use of application.properties?
  • It is used to define configuration settings such as server port, database URL, credentials, logging levels, etc.
How do you create a RESTful web service using Spring Boot?
  • Use @RestController, @RequestMapping, and other annotations to define REST endpoints and return data.