1. Difference between Spring and Spring Boot
Spring is an open-source lightweight framework that allows Java developers to build simple, reliable, and scalable enterprise applications. It simplifies the development of web applications compared to classic Java frameworks and APIs, such as JDBC, JSP, and Java Servlets. Here are key points about Spring:
The most important feature of the Spring Framework is dependency injection.
It helps to create a loosely coupled application.
To run a Spring application, developers need to set up the server explicitly.
Spring applications require developers to write a lot of code.
Spring Boot, on the other hand, is built on top of the Spring framework. It offers all the features of Spring and is easier to use. Spring Boot is a microservice-based framework that enables the creation of production-ready applications quickly. Key points about Spring Boot:
The most important feature of Spring Boot is autoconfiguration.
It helps to create a stand-alone application.
Comes with embedded servers like Tomcat, Jetty, or Undertow, eliminating the need for external server setup.
It reduces the lines of code significantly.
Spring Boot simplifies the development and deployment of Spring applications by offering a streamlined and opinionated approach, while the Spring Framework provides more flexibility and control for developers.
2. Main Features of Spring Boot
Auto Configuration:
Spring Boot uses the
@SpringBootApplication
annotation, which triggers auto-configuration.It sets up the application automatically based on the libraries in the classpath, removing the need to specify beans manually.
Standalone Nature:
Spring Boot applications are self-contained and can run independently without relying on external servers.
Embedded servers like Tomcat simplify deployment and facilitate microservices architecture.
3. Purpose of application.properties
or application.yml
This file is used to configure application properties such as:
Database settings
Server ports
Logging levels
4. Dependency Injection
Dependency Injection (DI) is a design pattern where objects are provided with their dependencies instead of creating them manually. This promotes loose coupling and simplifies testing and development.
5. Difference Between @Component
, @Service
, and @Repository
@Component
: A general-purpose annotation for Spring-managed beans. It marks a class as a Spring component, making it available for dependency injection.@Service
: A specialization of@Component
for service-layer beans, used to indicate business logic resides in the class.@Repository
: A specialization of@Component
for data access objects (DAOs). It translates persistence exceptions into Spring exceptions.
6. Advantages of Using Spring Boot
Simplified configuration
Embedded servers for easier deployment
Reduced development time
7. How Does Spring Boot Auto-Configuration Work?
Spring Boot auto-configures applications based on the dependencies present in the classpath. This eliminates the need for extensive manual configuration.
8. Purpose of @SpringBootApplication
Annotation
This annotation is a meta-annotation in Spring Boot that combines several critical Spring annotations:
Marks the main class as the configuration class.
Enables auto-configuration.
Reduces the need for manual setup of beans and dependencies.
9. Difference Between @Controller
and @RestController
@Controller
: Used for returning views like HTML.@RestController
: Returns JSON or other response bodies directly.
10. Purpose of @RequestMapping
Maps HTTP requests to handler methods in controllers.
11. Difference Between @PathVariable
and @RequestParam
@PathVariable
: Extracts values directly from the URL path.@RequestParam
: Retrieves values from query parameters appended to the URL.
12. Purpose of @Autowired
@Autowired
is used for automatic dependency injection. It allows Spring to wire required beans into your classes automatically.
13. What is Spring Data JPA?
Spring Data JPA provides a specification for persisting, reading, and managing data between Java objects and relational databases. It simplifies database interactions using standard interfaces.
14. Purpose of @Entity
Annotation
Marks a class as a JPA entity mapped to a database table.
15. Difference Between save()
and saveAndFlush()
in JPA
save()
: Saves the entity but doesn't immediately write to the database.saveAndFlush()
: Saves and immediately writes changes to the database.
16. What are RESTful Web Services?
RESTful web services use HTTP methods (GET, POST, PUT, DELETE) for CRUD operations and adhere to REST principles.
17. Purpose of @GetMapping
, @PostMapping
, @PutMapping
, and @DeleteMapping
These annotations map HTTP methods (GET, POST, PUT, DELETE) to specific handler methods in controllers.
18. Securing a Spring Boot Application
Use Spring Security to configure authentication and authorization. Features include:
Role-based access control
Custom permissions
Secure endpoints