Spring Boot Interview Questions and Answers
by Nithyanandham, on Sep 11, 2022 6:09:45 PM
Q1. What is Spring Boot?
Ans: Spring Boot is a brand new framework from the team at Pivotal, designed to simplify the bootstrapping and development of a new Spring application. The framework takes an opinionated approach to configuration, freeing developers from the need to define boilerplate configuration. In that, Boot aims to be a front-runner in the ever-expanding rapid application development space
Q2. What are Spring Boot Starter Projects?
Ans: Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.
Q3. What is Spring Boot Actuator?
Ans: Spring Boot Actuator is used by Spring Boot Framework to provide “Management EndPoints” to see Application Internals, Metrics etc
Q4. What is Spring Data?
Ans: Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services.
Q5. What is Spring Boot Starter?
Ans: Spring Boot Starters are just JAR Files. They are used by Spring Boot Framework to provide “Auto-Dependency Resolution”.
Q6. What is Spring Boot AutoConfigurator?
Ans: Spring Boot AutoConfigurator is used by Spring Boot Framework to provide “Auto-Configuration”.
Q7. Mention Spring Boot Components
Ans: Spring Boot Framework has the following components:
- Spring Boot Starter
- Spring Boot AutoConfigurator
- Spring Boot Actuator
- Spring Boot CLI
- Spring Boot Initilizr.
Q8. What is Spring Boot CLI?
Ans: In simple words, Spring Boot CLI is Auto Dependency Resolution, Auto-Configuration, Management EndPoints, Embedded HTTP Servers(Jetty,Tomcat etc.) and (Groovy,Auto-Imports)
Q9. What is Spring Boot Initilizr?
Ans: Spring Boot Initilizr is a Spring Boot tool to bootstrap Spring Boot or Spring Applications very easily.
Spring Boot Initilizr comes in the following forms:
- Spring Boot Initilizr With Web Interface
- Spring Boot Initilizr With IDEs/IDE Plugins
- Spring Boot Initilizr With Spring Boot CLI
- Spring Boot Initilizr With ThirdParty Tools
Q10. What is Spring Data REST?
Ans: Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories.
An example using JPA is shown below
@RepositoryRestResource(collectionResourceRel = "todos", path = "todos")
public interface TodoRepository
extends PagingAndSortingRepository<Todo, Long> {
Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.
Q11. What is the difference between RequestMapping and GetMapping?
Ans:
- RequestMapping is generic - you can use with GET, POST, PUT or any of the other request methods using the method attribute on the annotation.
- GetMapping is specific to GET request method. It’s just an extension of RequestMapping to improve clarity.
Q12. What and Why Embedded Servers?
Ans: Think about what you would need to be able to deploy your application (typically) on a virtual machine.
- Step 1 : Install Java
- Step 2 : Install the Web/Application Server (Tomcat/Websphere/Weblogic etc)
- Step 3 : Deploy the application wa
Q13. Dependencies can be classified into:
Ans:
- Spring - core, beans, context, aop
- Web MVC - (Spring MVC)
- Jackson - for JSON Binding
- Validation - Hibernate Validator, Validation API
- Embedded Servlet Container - Tomcat
- Logging - logback, slf4j
Q14. How does path=”users”, collectionResourceRel=”users” work with Spring Data Rest?
Ans: @RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRestRepository extends
PagingAndSortingRepository<User, Long>
- path - The path segment under which this resource is to be exported.
- collectionResourceRel - The rel value to use when generating links to the collection resource. This is used when generating HATEOAS links.
Q15. What are the other Starter Project Options that Spring Boot provides
Ans: Spring Boot also provides other starter projects including the typical dependencies to develop specific type of applications
- spring-boot-starter-web-services - SOAP Web Services
- spring-boot-starter-web - Web & RESTful applications
- spring-boot-starter-test - Unit testing and Integration Testing
- spring-boot-starter-jdbc - Traditional JDBC
- spring-boot-starter-hateoas - Add HATEOAS features to your services
- spring-boot-starter-security - Authentication and Authorization using Spring Security
- spring-boot-starter-data-jpa - Spring Data JPA with Hibernate
- spring-boot-starter-data-rest - Expose Simple REST Services using Spring Data REST
Q16. How does Spring enable creating production ready applications in quick time?
Ans: Spring Boot aims to enable production ready applications in quick time. Spring Boot provides a few non functional features out of the box like caching, logging, monitoring and embedded servers.
- spring-boot-starter-actuator - To use advanced features like monitoring & tracing to your application out of the box
- spring-boot-starter-undertow, spring-boot-starter-jetty, spring-boot-starter-tomcat - To pick your specific choice of Embedded Servlet Container
- spring-boot-starter-logging - For Logging using logback
- spring-boot-starter-cache - Enabling Spring Framework’s caching support
Q17. What is the minimum baseline Java Version for Spring Boot 2 and Spring 5?
Ans: Spring Boot 2.0 requires Java 8 or later. Java 6 and 7 are no longer supported.
Q19. What is Auto Configuration?
Ans: The problem with Spring and Spring MVC is the amount of configuration that is needed. <bean>
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/webjars/**" location="/webjars/"/>
Can we bring more intelligence into this? When a spring mvc jar is added into an application, can we auto configure some beans automatically?
Spring Boot looks at a) Frameworks available on the CLASSPATH b) Existing configuration for the application. Based on these, Spring Boot provides basic configuration needed to configure the application with these frameworks. This is called Auto Configuration.
Q20. What happens in the background when a Spring Boot Application is “Run as Java Application”?
Ans: If you are using Eclipse IDE, Eclipse maven plugin ensures that as soon as you add a dependency or make a change to the class file, it is compiled and ready in the target folder! And after that its just like any other Java application.
When you launch the java application, then the spring boot auto configuration magic kicks in.
- It launches up tomcat when it sees that you are developing a web application!
Q21. How do I change the package name of a project in Spring Initializer?
Ans: Good news is you can customise it. Click the link “Switch to the full version.“. You would be able to configure the package name you would want!
Q22. What is the difference between JPA and Hibernate?
Ans: Short Story
- JPA is a specification/Interface
- Hibernate is one of JPA implementations
When we use JPA, we use the annotation and interfaces from javax.persistence package, without using the hibernate import packages.
We recommend using JPA annotations as we are not tied to Hibernate as implementation. Later (I know - <1% Chance), we can use another JPA implementation.
Q23. Why do we need spring-boot-maven-plugin?
Ans: spring-boot-maven-plugin provides a few commands which enable you to package the code as a jar or run the application
- spring-boot:run runs your Spring Boot application.
- spring-boot:repackage repackages your jar/war to be executable.
- spring-boot:start and spring-boot:stop to manage the lifecycle of your Spring Boot application (i.e. for integration tests).
- spring-boot:build-info generates build information that can be used by the Actuator.
Q24. In which layer, should the boundary of a transaction start?
Ans: We recommend managing transactions in the Service layer. Logic for business transactions is in the business/service layer and you would want to enforce transaction management at that level
Q25. Why do we recommend not to use Spring Data Rest in real world applications?
Ans: We think Spring Data Rest is Good for quick prototyping! Be cautious about using this in Big applications!
With Spring Data REST you are exposing your database entitities directly as REST Services.
When you design RESTful services, Best design practices suggests that your interface should consider two important things
- Your Domain Model
- Your Consumers
With Spring Data REST, you are not considering either of those. You just expose entities as REST Services.
Thats why we suggest to use it for quick prototyping or the initial evolution of a project. It may not be a great idea for a fully evolved project.