0% found this document useful (0 votes)
14 views5 pages

Spring Boot Interview Notes

Uploaded by

Manasa P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Spring Boot Interview Notes

Uploaded by

Manasa P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Spring Boot Interview Notes

1. What is Spring Boot?


Spring Boot is a framework that simplifies the development of Java applications by:

- Providing auto-configuration

- Embedding servers (Tomcat/Jetty)

- Reducing boilerplate code

- Creating standalone applications

3. Types of Spring Annotations


Core Annotations:

- @Component: Generic component (bean)

- @Service: Service class (business logic)

- @Repository: DAO layer, handles database

- @Controller: Handles web requests

- @RestController: Combines @Controller and @ResponseBody

- @Configuration: Indicates Java-based configuration

- @Bean: Declares a bean in config class

- @Autowired: Auto-wires beans (Dependency Injection)

- @Qualifier: Helps choose between multiple beans

- @Value: Injects values from properties

- @RequestMapping, @GetMapping, @PostMapping: Handle HTTP requests

- @PathVariable: Gets variable from URL

- @RequestParam: Gets query parameter

- @RequestBody: Maps request body to Java object

- @ResponseBody: Sends Java object as HTTP response

- @ComponentScan: Scans for beans/components

- @EnableAutoConfiguration: Enables Spring Boot auto configuration


- @SpringBootApplication: Combines Configuration, EnableAutoConfiguration, and
ComponentScan

4. Spring MVC Architecture (Layers of Spring)


Layered Architecture:

- Controller Layer (@Controller/@RestController)

- Service Layer (@Service)

- DAO Layer (@Repository)

- Model Layer (Java classes)

- Database

Flow:

Client → Controller → Service → Repository → Database → back to Client

5. How Spring Boot Works (Step-by-Step Flow)


1. Main Class with @SpringBootApplication:

@SpringBootApplication

public class MyApp {

public static void main(String[] args) {

SpringApplication.run(MyApp.class, args);

2. Auto-Configuration: Based on dependencies

3. Embedded Server: Starts (Tomcat by default)

4. Bean Scanning: Detects and registers beans using @ComponentScan

5. REST Endpoints: Exposed via @RestController, @RequestMapping


6. Dependency Injection: @Autowired injects beans

7. Actuator (optional): Monitor metrics, health, etc.

🔁 Spring Boot Workflow (Step-by-Step)

1. Entry Point: Main Class


@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}

 @SpringBootApplication = Combines @Configuration,


@EnableAutoConfiguration, and @ComponentScan.
 This class is the starting point.

2. Auto-Configuration

Spring Boot automatically configures beans based on the dependencies present in


pom.xml or build.gradle.

Example: If you add spring-boot-starter-data-jpa, it auto-configures JPA


(Hibernate).

3. Component Scanning

Spring Boot scans packages for annotations like:

 @RestController
 @Service
 @Repository
 @Component

It creates beans for them and puts them into the Spring IoC container.
🗄️ How Spring Boot Connects to a Database

1. Add JDBC/JPA Dependency

In pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>

2. Configure application.properties or application.yml


spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=update

3. Create Entity Class


@Entity
public class Student {
@Id
@GeneratedValue
private Long id;
private String name;
}

4. Create Repository Interface


public interface StudentRepository extends JpaRepository<Student, Long>
{}

5. Create a Service and Controller


@Service
public class StudentService {
@Autowired
private StudentRepository repo;

public List<Student> getAll() {


return repo.findAll();
}
}
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService service;
@GetMapping
public List<Student> getStudents() {
return service.getAll();
}
}

You might also like