Spring & Spring Boot — Top 50 Interview Questions & Answers (With Simple Code)

 

✅ 1️⃣ Core Spring Framework — Interview Questions


1️⃣ What is Spring Framework?

Spring is a lightweight Java framework used to build loosely coupled, testable, and maintainable applications using IoC, DI, and AOP.


2️⃣ What is Inversion of Control (IoC)?

IoC means Spring Framework controls object creation, not the developer.

Without IoC:

Service s = new Service();

With IoC:
Spring creates and injects it automatically.


3️⃣ What is Dependency Injection (DI)?

DI provides required dependencies from outside instead of creating internally.

Example:

@Service public class OrderService { private final PaymentService paymentService; public OrderService(PaymentService paymentService) { this.paymentService = paymentService; } }

4️⃣ What is a Spring Bean?

A bean is an object managed by Spring Container.


5️⃣ Bean Scopes?

  • singleton (default)

  • prototype

  • request

  • session

Example:

@Component @Scope("prototype") class TestBean {}

6️⃣ What is @Component, @Service, @Repository, @Controller?

They are stereotype annotations:

  • @Component – generic bean

  • @Service – business logic

  • @Repository – database + exception translation

  • @Controller – MVC controller


7️⃣ What is @Autowired?

Used to inject dependencies automatically.


8️⃣ Difference between @Bean and @Component?

  • @Component → class level auto scanning

  • @Bean → method level manual bean creation


9️⃣ What is AOP in Spring?

Aspect Oriented Programming — handles cross-cutting concerns like logging, security.


🔟 What is @Transactional?

Handles transactions automatically.

@Transactional public void transfer() {}

1️⃣1️⃣ What is Spring MVC?

Framework for building web apps using MVC architecture.


1️⃣2️⃣ What is DispatcherServlet?

Front Controller in Spring MVC.


1️⃣3️⃣ What is ViewResolver?

Maps view names to actual pages.


1️⃣4️⃣ What are Profiles?

Used to define environments (dev/test/prod)


1️⃣5️⃣ What is Bean Lifecycle?

Bean creation → Dependency Injection → Initialization → Destruction


1️⃣6️⃣ What is @Lazy?

Delays bean creation until first use.

@Lazy @Component class Demo{}

1️⃣7️⃣ What is @Primary?

Used when multiple beans exist for same type.


1️⃣8️⃣ What is @Qualifier?

Used to choose specific bean.

@Autowired @Qualifier("emailService") Service service;

1️⃣9️⃣ What is ApplicationContext?

It is Spring’s IoC container.


2️⃣0️⃣ What is BeanFactory?

Basic container (ApplicationContext is advanced BeanFactory)



🚀 2️⃣ Spring Boot — Interview Questions


2️⃣1️⃣ What is Spring Boot?

Spring Boot simplifies Spring by providing:
✔ Auto Configuration
✔ Embedded Server
✔ Starter dependencies


2️⃣2️⃣ Difference — Spring vs Spring Boot?

SpringSpring Boot
Manual configAuto Config
Needs serverHas embedded Tomcat
Many dependenciesStarters simplify

2️⃣3️⃣ What is @SpringBootApplication?

Combination of:

  • @Configuration

  • @EnableAutoConfiguration

  • @ComponentScan


2️⃣4️⃣ How to create REST API?

@RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String hello() { return "Hello Spring Boot"; } }

2️⃣5️⃣ What are Starters?

Ready dependency packs like:

  • spring-boot-starter-web

  • spring-boot-starter-data-jpa

  • spring-boot-starter-test


2️⃣6️⃣ application.properties vs application.yml?

Both used for configuration.


2️⃣7️⃣ What is Spring Boot Actuator?

Provides health & monitoring endpoints.

management.endpoints.web.exposure.include=*

2️⃣8️⃣ What is Auto Configuration?

Spring Boot configures beans automatically based on dependencies.


2️⃣9️⃣ How to Change Server Port?

server.port=8081

3️⃣0️⃣ What is @RestController vs @Controller?

ControllerRestController
returns viewreturns JSON
uses @ResponseBody manuallybuilt-in


🗄 3️⃣ Spring Boot + Database / JPA


3️⃣1️⃣ How to configure DB?

spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root

3️⃣2️⃣ What is JPA Repository?

public interface UserRepo extends JpaRepository<User, Long> {}

3️⃣3️⃣ Entity Example

@Entity class User { @Id @GeneratedValue Long id; String name; }

3️⃣4️⃣ What is Hibernate?

ORM Framework used by JPA.


3️⃣5️⃣ What is @Entity?

Marks class as database table.


3️⃣6️⃣ What is @Id & @GeneratedValue?

Primary key + auto increment.


3️⃣7️⃣ Difference between save() and saveAll()?

save() — single record
saveAll() — multiple records



🔐 4️⃣ Spring Security


3️⃣8️⃣ What is Spring Security?

Framework for authentication + authorization.


3️⃣9️⃣ How to secure endpoint?

http.authorizeRequests() .anyRequest().authenticated() .and() .httpBasic();

4️⃣0️⃣ What is OAuth2?

Authorization framework.



🧪 5️⃣ Spring Boot Testing


4️⃣1️⃣ How to Unit Test?

@SpringBootTest class TestApp {}

4️⃣2️⃣ MockMVC Example

@Autowired MockMvc mockMvc;

4️⃣3️⃣ Test REST API

mockMvc.perform(get("/api/hello")) .andExpect(status().isOk());


⚙ 6️⃣ Advanced Spring Questions


4️⃣4️⃣ What is Microservices?

Small independent services communicating via REST.


4️⃣5️⃣ What is REST?

Representational State Transfer — Web API standard.


4️⃣6️⃣ What is Feign Client?

Used for calling other microservices.


4️⃣7️⃣ What is Eureka?

Service registry.


4️⃣8️⃣ What is Circuit Breaker?

Prevents failures in distributed systems.


4️⃣9️⃣ What is @Value?

Reads values from properties.

@Value("${app.name}") String name;

5️⃣0️⃣ Difference between Monolithic & Microservice?

MonolithicMicroservices
Single appMultiple small services
Hard to scaleEasy to scale
Single failure kills appIndependent failure handling

No comments:

Post a Comment

Optimistic vs Pessimistic Locking in Spring (with Multi-Instance Microservices)

  Optimistic vs Pessimistic Locking in Spring (with Multi-Instance Microservices) When multiple users (or services) try to update the same ...

Featured Posts