December 08, 2017

Spring Boot Interview Questions


In this post, we will look at Spring Boot Interview questions. Examples are provided with an explanation. 

Q: What is Spring Boot?
A: Over the years spring has become more and more complex as new functionalities have been added. Just visit the page-https://spring.io/projects and we will see all the spring projects we can use in our application for different functionalities. If one has to start a new spring project we have to add build path or add maven dependencies, configure application server, add spring configuration. So a lot of effort is required to start a new spring project as we have to currently do everything from scratch. Spring Boot is the solution to this problem. Spring boot has been built on top of existing spring framework. Using spring boot we avoid all the boilerplate code and configurations that we had to do previously. Spring boot thus helps us use the existing Spring functionalities more robustly and with minimum efforts.
More details and miscellaneous examples 

Q: What are advantages of Spring Boot?
A:
 The advantages of Spring Boot are
  • Reduce Development, Testing time and efforts.
  • Use of JavaConfig helps avoid usage of XML.
  • Avoid lots of maven imports and the various version conflicts.
  • Provide Opinionated Development approach.
  • Quick start to development by providing defaults.
  • No Separate Web Server Needed.Which means that you no longer have to boot up Tomcat, Glassfish, or anything else.
  • Requires less configuration-Since there is no web.xml file. Simply add classes annotated with@Configuration and then you can add methods annotated with@Bean, and Spring will automatically load up the object and manage it like it always has. You can even add @Autowired to the bean method to have Spring autowire in dependencies needed for the bean.
  • Environment Based Configuration-Using these properties, you can pass into the application which environment you are using with:-Dspring.profiles.active={enviornment}. Spring will then load up the subsequent application properties file at (application-{environment}.properties) after loading up the main application properties file.


Q: Which build tool have you used to develop Spring Boot Application?
A: 
Spring Boot application can be developed using Maven as well as Gradle. 
Spring Boot application using Maven 
Spring Boot application using Gradle 

Q: What is JavaConfig?
A: 
Spring JavaConfig is a product of the Spring community that provides a pure-Java approach to configuring the Spring IoC Container. It thus helps avoid using XML configurations. The advantages of using JavaConfig are
The advantages of JavaConfig are
  • Object-oriented configuration. Because configurations are defined as classes in JavaConfig, users can take full advantage of object-oriented features in Java. One configuration class may subclass another, overriding it's @Bean methods, etc.
  • Reduced or eliminated XML configuration. The benefits of externalized configuration based on the principles of dependency injection have been proven. However, many developers would prefer not to switch back and forth between XML and Java. JavaConfig provides developers with a pure-Java approach to configuring the Spring container that is conceptually similar to XML configuration. It is technically possible to configure the container using only JavaConfig configuration classes, however in practice, many have found it ideal to mix-and-match JavaConfig with XML.
  • Type-safe and refactoring-friendly. JavaConfig provides a type-safe approach to configuring the Spring container. Thanks to Java 5.0's support for generics, it is now possible to retrieve beans by type rather than by name, free of any casting or string-based lookups.
Added security configuration without XML using java config.
Q: How to reload my changes on Spring Boot without having to restart the server?
A: 
This can be achieved using DEV Tools. With this dependency any changes you save, the embedded tomcat will restart. Spring Boot has a Developer tool (DevTools) module which helps to improve the productivity of developers. One of the key challenges for the Java developers is to auto-deploy the file changes to server and auto restart the server. Developers can reload changes on Spring Boot without having to restart my server. This will eliminate the need for manually deploying the changes every time. Spring Boot doesn’t have this feature when it has released it’s first version. This was a most requested feature for the developers. The module DevTools does exactly what is needed for the developers. This module will be disabled in the production environment. It also provides an H2-database console for better testing the application. The following dependency is used
   
   org.springframework.boot
   spring-boot-devtools
   true
    
   
DevTool dependency usage for autostart and H2 DB console is illustrated in this example

QWhat is Actuator in Spring Boot?
A: 
Spring boot actuator is one of the important features in spring boot framework. Spring boot actuator helps you to access the current state of the running application in production environment. There are several metrics that have to be checked and monitored in the production environment. Even some external applications may be using those services to trigger the alert message to the concerned person. Actuator module exposes set of REST endpoints that can be directly accessed as an HTTP URL to check the status.

Q: How to run Spring boot application to custom port?
A: 
In order to run a spring boot application on a custom port you can specify the port in application.properties.
server.port=8090 

Q: Have you written Test cases using Spring Boot?
A: 
Spring Boot provides the @SpringBootTest for writing Unit Test Cases
Spring Boot Unit Test Simple Example

Q: What is YAML?
A: 
YAML is a human-readable data serialization language. It is commonly used for configuration files.
Compared to the properties file, YAML file is much more structured and less confusing in case we want to add complex properties in the configuration file. As can be seen, YAML has hierarchical configuration data. 
Use YAML properties in Spring Boot
Q: How to implement security for Spring boot application?
A: 
For Implementing security for Spring Boot we use the spring-boot-starter-security dependency and have to add the Security config. It requires very little code. Config class will have to extend WebSecurityConfigurerAdapter and override its methods.
Spring Boot Security example and explanation 

Q: Have you integrated Spring Boot and ActiveMQ?
A: 
For integrating Spring Boot and ActiveMQ we use the spring-boot-starter-activemq dependency
It requires very little configuration and no boilerplate code.
Spring Boot ActiveMQ explanation 

Q: Have you integrated Spring Boot and Apache Kafka?
A: 
For integrating Spring Boot and Apache Kafka we use the spring-kafka dependency.
Spring Boot + Apache Kafka Example 

Q: How to implement Pagination and Sorting with Spring Boot?
A: 
Using Spring Boot achieving pagination is very simple. Using the Spring Data-JPA this is achieved passing pageable org.springframework.data.domain.Pageable to the repository methods.
Spring Boot pagination explanation

Q: What is Swagger? Have you implemented it using Spring Boot?
A: 
Swagger is widely used for visualizing APIs, and with Swagger UI it provides online sandbox for frontend developers. For the tutorial, we will use the Springfox implementation of the Swagger 2 specification. A swagger is a tool, a specification and a complete framework implementation for producing the visual representation of RESTful Web Services. It enables documentation to be updated at the same pace as the server. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Thus Swagger removes the guesswork in calling the service.
Spring Boot + Swagger2 

Q: What is Spring Profiles? How do you implement it using Spring Boot?
A: 
Spring Profiles allows users to register beans depending on the profile(dev, test, prod etc). So when the application is running in DEVELOPMENT only certain beans can be loaded and when in PRODUCTION certain other beans can be loaded. Suppose our requirement is that the Swagger documentation be enabled only for the QA environment and disabled for all others. This can be done using Profiles. Spring Boot makes using Profiles very easy. 
Spring Boot + profiles 

Q: What is Spring Batch? How do you implement it using Spring Boot?
A: 
Spring Boot Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. It also provides more advanced technical services and features that will enable extremely high-volume and high-performance batch jobs through optimization and partitioning techniques.Simple as well as complex, high-volume batch jobs can leverage the framework in a highly scalable manner to process significant volumes of information. 
Spring Boot + Batch 

Q: What is FreeMarker Template? How do you implement it using Spring Boot?
A: 
FreeMarker is a Java-based Template Engine, originally focusing on dynamic web page generation with MVC software architecture. The major advantage of using Freemarker is the complete separation of the Presentation layer and the Business Layer. The Programmers can work on the application code while the designers can work on the HTML page design. Finally using freemarker these can then be combined to give the final output page. 
Spring Boot + FreeMarker Example 

Q: How did you perform database operations using Spring Boot?
A: 
Spring Boot Tutorial-Spring Data JPA
Spring Boot JDBC Example 

Q: How to upload a file using Spring?
A: 
Spring Boot + File Upload Example
Q: How to implement interceptors with Spring Boot?
A: 
Using Spring MVC HandlerInterceptor with Spring Boot 

Q: How to use schedulers with Spring Boot?
A: 
Spring Boot Task Scheduler Example 

Q: Which all starter maven dependencies have you used?
A: 
Have used different starter dependencies like spring-boot-starter-ActiveMQ dependencyspring-boot-starter-security dependencyspring-boot-starter-web dependency 
. This helps in adding less number of dependencies and also in reducing version conflicts. 
Spring Boot Security example and explanation 

Q: Have you used any integration framework with Spring Boot?
A:
 Have integrated Apache Camel with Spring Boot. Made use of Apache Camel Spring Boot starter dependency. Spring Boot +Apache Camel 

Q: What is Apache Freemarker? When to use it instead of JSP? How to integrate it with Spring Boot?
A:
JSP is tailor-made for Web pages, Freemarker template is a more generic templating language - it can be used to generate HTML, plain text, emails, etc.
Spring Boot + FreeMarker Example

Q: What is AOP? How to use it with Spring Boot?
A: 
During software development, functions that span multiple points of an application are called cross-cutting concerns. These cross-cutting concerns differ from the application’s main business logic. Hence, separating these cross-cutting concerns from the business logic is where aspect-oriented programming (AOP) comes into the picture.
Spring Boot + AOP Example 
Q: What is Apache Kafka? How to integrate it with Spring Boot?
A:
Apache Kafka is a distributed publish-subscribe messaging system. It is a scalable, fault-tolerant, publish-subscribe messaging system which enables us to build distributed applications. It is an Apache Top-Level project. Kafka is suitable for both offline and online message consumption.
Spring Boot + Apache Kafka Example 

Q: Have you used any Spring Cloud Components with Spring Boot?
A:
 Have used Spring Cloud components like Netflix Eureka for Service Registration, Ribbon for Load Balancing.
Spring Boot + Cloud Components


Reference: http://www.javainuse.com/spring/SpringBootInterviewQuestions

1 comment:

  1. Nice article . this is very good information. keep this great job up. visit spring boot tutorials

    ReplyDelete

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts