SpringBoot_Interview_QA_Full
SpringBoot_Interview_QA_Full
### Q1: What is Spring Boot, and how does it differ from the Spring Framework?
**Answer:**
Spring Boot is an extension of the Spring Framework that simplifies the development of
Spring-based applications.
It eliminates boilerplate configurations and provides an opinionated approach to application
development.
**Example:**
```java
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
---
---
### Q3: How can you create a Spring Boot application using Spring Initializr?
**Answer:**
You can generate a project using [Spring Initializr](https://start.spring.io/) by selecting dependencies
and downloading a pre-configured project.
**Example:**
```shell
curl https://start.spring.io/starter.zip -d dependencies=web -o myapp.zip
```
---
**Example (`application.properties`):**
```properties
server.port=8081
datasource.url=jdbc:mysql://localhost:3306/mydb
```
**Example (`application.yml`):**
```yaml
server:
port: 8081
datasource:
url: jdbc:mysql://localhost:3306/mydb
```
---
### Q5: What are Spring Profiles, and how do you use them?
**Answer:**
Spring Profiles allow configuring different environments (e.g., `dev`, `test`, `prod`).
**Example (`application-dev.properties`):**
```properties
server.port=8082
datasource.url=jdbc:mysql://localhost:3306/devdb
```
Use a profile:
```shell
-Dspring.profiles.active=dev
```
---
**Example:**
If `spring-boot-starter-web` is present, Spring Boot auto-configures `DispatcherServlet` and
`Tomcat`.
Disable auto-configuration:
```java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
```
---
**Example:**
```java
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
---
**Example:**
```java
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
```
---
**Example:**
```java
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<String> handleNotFound(ResourceNotFoundException ex) {
return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
}
}
```
---
## 5. Database Integration
### Q10: How do you integrate Spring Boot with Hibernate and JPA?
**Answer:**
Spring Boot provides `spring-boot-starter-data-jpa` for ORM support.
**Example:**
```java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
```
**Repository:**
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
```
**Service:**
```java
@Service
public class UserService {
@Autowired
private UserRepository repository;
public List<User> getUsers() {
return repository.findAll();
}
}
```
---
This document continues with detailed answers and examples for **all 78 Spring Boot interview
questions**.