Hyder Abbas
1. What is Spring Boot and why is it preferred over the traditional
Spring Framework?
Spring Boot makes it easy to create Spring applications by removing a lot of setup. It
auto-configures things, so you can run your app with less code and configuration. It has
an embedded server (like Tomcat), so no need to deploy separately.
2. Explain the purpose of the @SpringBootApplication annotation.
o
It combines 3 annotations: @Configuration, @EnableAutoConfiguration, and
@ComponentScan. It tells Spring Boot to start the app, scan components, and apply
sk
default configs.
3. How does Spring Boot's auto-configuration work?
Spring Boot looks at the dependencies in your project and tries to configure beans
lu
automatically based on that. For example, if you add Spring Web, it auto-configures a
web server.
4. What are Spring Boot Starters? Give an example.
Te
Starters are pre-made packages with needed dependencies. For example,
spring-boot-starter-web gives you everything needed to build a web app.
5. What is the difference between @Controller and
@RestController?
@Controller returns a view (like HTML). @RestController returns data (like
JSON) directly. @RestController = @Controller + @ResponseBody.
6. Explain @RequestMapping, @GetMapping, and @PostMapping.
o
All map HTTP requests to methods:
● @RequestMapping can handle any type of HTTP request.
sk
● @GetMapping is for GET requests.
● @PostMapping is for POST requests.
7. How do you handle request parameters and path variables in a
REST controller?
lu
● Use @RequestParam for query parameters (e.g., ?name=John)
● Use @PathVariable for URL parts (e.g., /user/5)
Te
8. What is the difference between @RequestParam and
@PathVariable?
● @RequestParam gets values from the URL after ?
● @PathVariable gets values directly from the URL path
9. What is Spring Boot Actuator? Name some endpoints.
It provides ready-to-use endpoints to monitor and manage your app. Examples:
/actuator/health, /actuator/info, /actuator/metrics.
10. What are Spring Profiles and why are they used?
Profiles let you define different settings for different environments (dev, test,
prod). You can separate configurations.
11. How do you activate a specific profile?
You can set it in application.properties:
properties
spring.profiles.active=dev
o
Or pass as a command-line argument:
sk
--spring.profiles.active=dev
12. What are the different scopes of a Spring bean?
lu
Simplified Answer:
● singleton: One instance (default)
● prototype: New instance each time
● request: One per HTTP request (web only)
Te
● session: One per session (web only)
13. What is Spring Data JPA?
It simplifies working with databases using JPA. You just create interfaces, and Spring
handles the queries for you.
14. What are the key interfaces in Spring Data JPA?
● JpaRepository
● CrudRepository
● PagingAndSortingRepository
15. How do you perform a custom query with Spring Data JPA?
Use the @Query annotation in your repository:
@Query("SELECT u FROM User u WHERE u.name = :name")
o
List<User> findByName(@Param("name") String name);
sk
16. What is AOP and why is it used in Spring?
AOP = Aspect-Oriented Programming. It lets you add cross-cutting concerns like
logging, security, or transactions without changing business logic.
17. What are the key AOP concepts?
lu
● Aspect: Logic you want to apply (e.g., logging)
● Join Point: Where to apply it (e.g., method call)
● Advice: The actual code
● Pointcut: When/where advice applies
Te
18. What is Spring Security and its core features?
Spring Security handles authentication and authorization. It helps protect endpoints and
manage user roles, login, etc.
19. Explain the Spring Security Filter Chain.
It’s a chain of filters that check each request (like login check, authorization, etc.). Each
filter performs a specific task.
Hyder Abbas
20. What’s the difference between authentication and authorization?
● Authentication: Who are you? (Login)
● Authorization: What can you do? (Permissions)
21. What is the @Transactional annotation used for?
It ensures that a method runs inside a database transaction. If something fails, it rolls
o
back the changes.
22. How do you handle exceptions in a Spring Boot REST API?
sk
Use @ControllerAdvice with @ExceptionHandler to catch and return custom
error responses.
lu
23. What is the purpose of @Qualifier?
When there are multiple beans of the same type, use @Qualifier to choose which
one to inject.
Te
24. What is the @Value annotation?
It injects values from property files into fields:
@Value("${app.name}")
private String appName;
25. What is the purpose of spring-boot-devtools?
It helps during development by auto-restarting the app and enabling live reload when
files change.
26. Explain Dependency Injection (DI) and Inversion of Control (IoC).
IoC means the framework controls object creation. DI means Spring injects
dependencies (objects) where needed.
27. What are the ways to perform DI? Which is preferred?
o
● Constructor injection (Recommended)
sk
● Setter injection (Recommended)
● Field injection (Not recommended)
28. How does Spring Boot handle caching?
lu
Use @EnableCaching and @Cacheable annotations. Spring stores results so it
doesn’t redo the same work.
29. What is Spring Boot DevTools?
Te
DevTools auto-restarts the app, enables live reload, and helps developers work faster
during coding.
30. How do you integrate Swagger/OpenAPI in Spring Boot?
Add springdoc-openapi dependency. It auto-generates API documentation at
/swagger-ui.html. Helps in testing and sharing APIs.
31. What is the difference between application.properties and
application.yml?
Both are used to configure Spring Boot apps.
● application.properties uses key-value pairs.
● application.yml uses YAML format, which is more structured and readable.
32. How can you externalize configuration in Spring Boot?
o
You can move settings (like DB URL, credentials) to files like
application.properties, environment variables, or command-line arguments. This
sk
avoids hardcoding in your code.
33. What is @Component annotation?
It tells Spring that the class is a bean and should be managed by Spring’s container.
lu
Spring will auto-detect it and create an object.
34. What is the difference between @Component, @Service, and
@Repository?
Te
All are Spring-managed beans.
● @Component: Generic bean
● @Service: Used for service classes (business logic)
● @Repository: Used for database access layer
35. What is the use of CommandLineRunner?
It runs a block of code after the application starts. Useful for initial setup or testing.
Hyder Abbas
36. What is the difference between spring-boot-starter-parent
and spring-boot-dependencies?
● starter-parent: Provides a default build setup (plugins, Java version, etc.)
● dependencies: Manages versions of libraries so you don’t have to specify them
37. How do you define a custom exception in Spring Boot?
o
Create a new class that extends RuntimeException. You can then throw it in your
code and handle it with @ControllerAdvice.
sk
38. What is the use of @EnableAutoConfiguration?
It tells Spring Boot to guess the configuration based on the dependencies you have in
the project.
lu
39. What is the difference between @Bean and @Component?
Te
● @Component: Automatically picked up by component scan.
● @Bean: You manually declare a bean in a config class.
40. What is a DTO in Spring?
DTO (Data Transfer Object) is a plain class used to transfer data between layers without
exposing entity objects directly.
41. How does Spring Boot support file uploads?
Use MultipartFile in your controller method to accept files. Add
spring-boot-starter-web and set file size limits in config if needed.
42. What is a RestTemplate?
It’s a Spring class used to make HTTP requests (like GET, POST) from one Spring app
to another.
o
43. What is the use of ModelAndView?
Used in Spring MVC to return both model data and view name in one object (mostly in
sk
traditional MVC, not REST APIs).
44. What is the difference between
spring.jpa.hibernate.ddl-auto=update and create?
lu
● update: Updates DB tables without deleting data
● create: Drops and recreates tables every time (data loss)
Te
45. What is CORS and how do you enable it in Spring Boot?
CORS (Cross-Origin Resource Sharing) allows front-end apps from other domains to
access your API. Use @CrossOrigin annotation to enable it.
46. How do you secure a specific endpoint in Spring Security?
In your security config class, override configure(HttpSecurity http) and define
which URLs need authentication.
Hyder Abbas
47. What is the use of @Scheduled annotation?
It runs methods automatically at fixed times or intervals. Used for tasks like sending
emails, backups, etc.
48. How do you enable scheduling in Spring Boot?
o
Add @EnableScheduling in your main class and use @Scheduled on methods you
want to run on a schedule.
sk
49. What is the use of application-{profile}.properties?
It allows separate config files for each profile. For example:
lu
● application-dev.properties for development
● application-prod.properties for production
50. How do you return a custom HTTP status in a Spring REST
controller?
Te
Use ResponseEntity to set a custom response body and status:
return new ResponseEntity<>("Not Found", HttpStatus.NOT_FOUND);