Q1.
Spring vs Spring Boot
Spring Spring Boot
A web application framework based
A module of Spring
on Java
Provides tools and libraries to create Used to create a Spring application
customized web applications project which can just run/ execute
Spring is more complex than Spring Spring Boot is less complex than
Boot the Spring framework
Takes an opinionated view of a
Takes an unopinionated view
platform
Q2. What is Spring Boot and mention the need for it?
Spring Boot is a Spring module that aims to simplify the use of the Spring
framework for Java development. It is used to create stand-alone Spring-
based applications that you can just run. So, it basically removes a lot of
configurations and dependencies. Aiming at the Rapid Application
Development, Spring Boot framework comes with the auto-dependency
resolution, embedded HTTP servers, auto-configuration, management
endpoints, and Spring Boot CLI.
So, if you ask me why should anybody use Spring Boot, then I would say,
Spring Boot not only improves productivity but also provides a lot of
conveniences to write your own business logic.
Q3. Mention the advantages of Spring Boot
The advantages of Spring Boot are as follows:
Provides auto-configuration to load a set of default configuration for
a quick start of the application
Creates stand-alone applications with a range of non-functional
features that are common to large classes of projects
It comes with embedded tomcat, servlet containers jetty to avoid
the usage of WAR files
Spring Boot provides an opinionated view to reduce the developer
effort and simplify maven configurations
Provides CLI tool to develop and test applications
Comes with Spring Boot starters to ensure dependency
management and also provides various security metrics
Consists of a wide range of APIs for monitoring and managing
applications in dev and prod.
Integrates with Spring Ecosystem like Spring JDBC, Spring ORM,
Spring Data, Spring Security easily by avoiding boilerplate code.
Q4. Mention a few features of Spring Boot.
Few important features of Spring Boot are as follows:
1. Spring CLI – Spring Boot CLI allows you to Groovy for writing Spring
boot application and avoids boilerplate code.
2. Starter Dependency – With the help of this feature, Spring Boot
aggregates common dependencies together and eventually
improves productivity
3. Auto-Configuration – The auto-configuration feature of Spring Boot
helps in loading the default configurations according to the project
you are working on. In this way, you can avoid
any unnecessary WAR files.
4. Spring Initializer – This is basically a web application, which can
create an internal project structure for you. So, you do not have to
manually set up the structure of the project, instead, you can use
this feature.
5. Spring Actuator – This feature provides help while running Spring
Boot applications.
6. Logging and Security – The logging and security feature of Spring
Boot, ensures that all the applications made using Spring Boot are
properly secured without any hassle.
Q5. Explain how to create a Spring Boot application using
Maven.
Well, there are various approaches to create a Spring Boot
application using maven, but if I have to name a few, then following are
the ways to create a Spring Boot project/ application using maven:
Spring Boot CLI
Spring Starter Project Wizard
Spring Initializr
Spring Maven Project
Q6. Mention the possible sources of external
configuration.
There is no doubt in the fact that Spring Boot allows the developers to run
the same application in different environments. Well, this is done with the
support it provides for external configuration. It uses environment
variables, properties files, command-line arguments, YAML files, and
system properties to mention the required configuration properties. Also,
the @value annotation is used to gain access to the properties. So, the
most possible sources of external configuration are as follows:
Application Properties – By default, Spring Boot searches for the
application properties file or its YAML file in the current directory,
classpath root or config directory to load the properties.
Command-line properties – Spring Boot provides command-line
arguments and converts these arguments to properties. Then it
adds them to the set of environment properties.
Profile-specific properties – These properties are loaded from
the application-{profile}.properties file or its YAML file. This file
resides in the same location as that of the non-specific property files
and the{profile} placeholder refers to an active profile.
Q7. Can you explain what happens in the background
when a Spring Boot Application is “Run as Java
Application”?
When a Spring Boot application is executed as “Run as Java application”,
then it automatically launches up the tomcat server as soon as it sees,
that you are developing a web application. To learn more about Java, it’s
recommended to join Java training course today at Edureka.
Q8. What are the Spring Boot starters and what are
available the starters?
Spring Boot starters are a set of convenient dependency management
providers that can be used in the application to enable dependencies.
These starters, make development easy and rapid. All the available
starters come under the org.springframework.boot group. Few of the
popular starters are as follows:
spring-boot-starter: – This is the core starter and includes logging,
auto-configuration support, and YAML.
spring-boot-starter-jdbc – This starter is used for HikariCP
connection pool with JDBC
spring-boot-starter-web – Is the starter for building web applications,
including RESTful, applications using Spring MVC
spring-boot-starter-data-jpa – Is the starter to use Spring Data JPA
with Hibernate
spring-boot-starter-security – Is the starter used for Spring Security
spring-boot-starter-aop: This starter is used for aspect-oriented
programming with AspectJ and Spring AOP
spring-boot-starter-test: Is the starter for testing Spring Boot
applications
Q9. Explain Spring Actuator and its advantages.
Spring Actuator is a cool feature of Spring Boot with the help of which you
can see what is happening inside a running application. So, whenever you
want to debug your application, and need to analyze the logs you need to
understand what is happening in the application right? In such a scenario,
the Spring Actuator provides easy access to features such as identifying
beans, CPU usage, etc. The Spring Actuator provides a very easy way to
access the production-ready REST points and fetch all kinds of information
from the web. These points are secured using Spring Security’s content
negotiation strategy.
Q10. What is Spring Boot dependency management?
Spring Boot dependency management is basically used to manage
dependencies and configuration automatically without you specifying the
version for any of that dependencies.
Q12. Explain what is thymeleaf and how to use thymeleaf?
Thymeleaf is a server-side Java template engine used for web
applications. It aims to bring natural template for your web application
and can integrate well with Spring Framework and HTML5 Java web
applications. To use Thymeleaf, you need to add the following code in the
pom.xml file:
1<dependency>
2<groupId>org.springframework.boot</groupId>
3<artifactId>spring-boot-starter-thymeleaf</artifactId>
4</dependency>
Q13. Can we change the port of the embedded Tomcat
server in Spring boot?
Yes, we can change the port of the embedded tomcat server by using the
application properties file. In this file, you have to add a property of
“server.port” and assign it to any port you wish to. For example, if you
want to assign it to 8081, then you have to mention server.port=8081.
Once you mention the port number, the application properties file will be
automatically loaded by Spring Boot and the required configurations will
be applied on to the application.
Q14. What is the need for Spring Boot DevTools?
Spring Boot Dev Tools are an elaborated set of tools and aims to make the
process of developing an application easier. If the application runs in the
production, then this module is automatically disabled, repackaging of
archives are also excluded by default. So, the Spring Boot Developer Tools
applies properties to the respective development environments. To
include the DevTools, you just have to add the following dependency into
the pom.xml file:
1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-devtools</artifactId>
4</dependency>
Q15. Mention the steps to create a Spring Boot project
using Spring Initializer.
Spring Initializr is a web tool provided by Spring. With the help of this tool,
you can create Spring Boot projects by just providing project details. The
following steps need to be followed to create a Spring Boot project using
Spring Initializer:
Choose the maven project and the required dependencies. Then, fill
in the other required details like Group, Artifact, and then click on
Generate Project.
Once the project is downloaded, extract the project onto your
system
Next, you have to import this project using the import option on the
Spring Tool Suite IDE
While importing the project, remember that you have to
choose the project type to be Maven and the source project
should contain the pom.xml file.
Once, all the above steps are followed you will see that the Spring Boot
project is created with all the required dependencies.
Q16. Mention the steps to connect Spring Boot application
to a database using JDBC.
Spring Boot starter projects provide the required libraries to connect the
application with JDBC. So, for example, if you just have to create an
application and connect it with MySQL database, you can follow the below
steps:
Step 1: Create a database in MySQL
1CREATE DATABASE example;
Step 2: Then you have to create a table inside this database.
1CREATE TABLE customers(customerid INT PRIMARY KEY NOT NULL AUTO_INCREMENT, customernam
Step 3: Now, create a Spring Boot project and provide the required
details
Step 4: Add the JDBC, MySQL and web dependencies.
Step 5: Once the project is created, you have to configure the database
into application properties
1spring.datasource.url=jdbc:mysql://localhost:3306/example
2spring.datasource.username=root
3spring.datasource.password=edureka
4spring.jpa.hibernate.ddl-auto=create-drop
Step 6: The main application.java class should have the following code:
1package com.edureka;
2import org.springframework.boot.SpringApplication;
3import org.springframework.boot.autoconfigure.SpringBootApplication;
4@SpringBootApplication
5public class SampleApplication {
6 public static void main(String[] args) {
7 SpringApplication.run(SampleApplication.class, args);
8 }
9}
Spring Boot Interview Questions
Q31. Mention the advantages of the YAML file than
Properties file and the different ways to load YAML file in
Spring boot.
The advantages of the YAML file than a properties file is that the data is stored in a
hierarchical format. So, it becomes very easy for the developers to debug if there is an issue.
The SpringApplication class supports the YAML file as an alternative to properties whenever
you use the SnakeYAML library on your classpath. The different ways to load a YAML file
in Spring Boot is as follows:
Use YamlMapFactoryBean to load YAML as a Map
Use YamlPropertiesFactoryBean to load YAML as Properties
Q32. How is Hibernate chosen as the default
implementation for JPA without any configuration?
When we use the Spring Boot Auto Configuration, automatically the spring-boot-starter-
data-jpa dependency gets added to the pom.xml file. Now, since this dependency has a
transitive dependency on JPA and Hibernate, Spring Boot automatically auto-configures
Hibernate as the default implementation for JPA, whenever it sees Hibernate in the classpath.
Q33. What do you understand by Spring Data REST?
Spring Data REST is used to expose the RESTful resources around Spring Data repositories.
Consider the following example:
1@RepositoryRestResource(collectionResourceRel = "sample", path = "sample
2public interface SampleRepository
3 extends CustomerRepository<sample, Long> {
Now, to expose the REST services, you can use the POST method in the following way:
1{
2"customername": "Rohit"
3}
Response Content
1{
"customername": "Rohit"
2"_links": {
3"self": {
4"href": "<a href="http://localhost:8080/sample/1">http://localhost:8080
5a>"
6},
7"sample": {
8"href": "<a href="http://localhost:8080/sample/1">http://localhost:8080
a>"
9
}
1
0}
Observe that the response content contains the href of the newly created resource.
Q34. What is the difference between RequestMapping and
GetMapping?
The @GetMapping is a composed annotation that acts as a shortcut
for @RequestMapping(method = RequestMethod.GET). Both these methods support the
consumes. The consume options are :
consumes = “text/plain”
consumes = {“text/plain”, “application/*”}
Q35. In which layer, should the boundary of a transaction
start?
The boundary of the transaction should start from the Service Layer since the logic for the
business transaction is present in this layer itself.
Q36. How does path=”sample”,
collectionResourceRel=”sample” work with Spring Data
Rest?
1@RepositoryRestResource(collectionResourceRel = "sample", path = "sample
2public interface SampleRepository extends
3PagingAndSortingRepository<Sample, Long>
path – This section is used to mention the segment under which the resource is to be
exported.
collectionResourceRel – This value is used to generate links to the collection resource.
Q37. Explain how to register a custom auto-configuration.
In order to register an auto-configuration class, you have to mention the fully-qualified name
under the @EnableAutoConfiguration key META-INF/spring. factories file. Also, if we build
the with maven, then this file should be placed in the resources/META-INT directory.
Q38. How do you Configure Log4j for logging?
Since Spring Boot supports Log4j2 for logging a configuration, you have to exclude Logback
and include Log4j2 for logging. This can be only done if you are using the starters project.
Q39. Mention the differences between WAR and embedded
containers
WAR Embedded Containers
WAR benefits a considerable measure
from Spring Boot
2) Mention some advantages of Spring Boot
Here are some major advantages of using spring-boot:
Helps you to create a stand-alone application, which can be
started using java.jar.
It offers pinpointed‘started’ POMs to Maven configuration.
Allows you to Embed Undertow, Tomcat, or Jetty directly.
Helps you to configure spring whenever possible automatically.
What is Spring Boot starter? How is it useful?
Spring Boot has many starters. They are a set of convenient dependency
descriptors. Starter allows you to include these descriptors in your
pom.xml.
For example, If you want to work with Spring MVC, you can include
“spring–boot–starter–web” as a dependency in pom.xml.
What are the essential components of Spring Boot
The important components of Spring Boot are:
Spring Boot Starter
Spring Boot autoconfiguration
Spring Boot Actuator
Spring Boot CLI
What is @pathVariable?
@PathVariable annotation helps you to extract information from the
URI directly.
What are the major differences between
RequestMapping and GetMapping?
RequestMapping can be used with GET, POST, PUT, and many other
request methods using the method attribute on the annotation.
Whereas GetMapping is only an extension of RequestMapping, which
helps you to improve clarity on requests.
What are the advantages of micro service?
Following are the major advantages of micro service:
It makes development fast and easy.
Compatible with all container.
Reduce production time.
It’s a lightweight model that supports a major business
application.
Spring Boot Interview Questions For Freshers
1. What are the advantages of using Spring Boot?
The advantages of Spring Boot are listed below:
Easy to understand and develop spring applications.
Spring Boot is nothing but an existing framework with the addition of an
embedded HTTP server and annotation configuration which makes it
easier to understand and faster the process of development.
Increases productivity and reduces development time.
Minimum configuration.
We don’t need to write any XML configuration, only a few annotations are
required to do the configuration.
2. What are the Spring Boot key components?
Below are the four key components of spring-boot:
Spring Boot auto-configuration.
Spring Boot CLI.
Spring Boot starter POMs.
Spring Boot Actuators.
3. Why Spring Boot over Spring?
Below are some key points which spring boot offers but spring doesn’t:
Starter POM.
Version Management.
Auto Configuration.
Component Scanning.
Embedded server.
InMemory DB.
Actuators
4. What is the starter dependency of the Spring boot
module?
Spring boot provides numbers of starter dependency, here are the most
commonly used -
Data JPA starter.
Test Starter.
Security starter.
Web starter.
Mail starter.
Thymeleaf starter.
5. How does Spring Boot works?
Spring Boot automatically configures your application based on the
dependencies you have added to the project by using annotation. The
entry point of the spring boot application is the class that contains
@SpringBootApplication annotation and the main method.
Spring Boot automatically scans all the components included in the
project by using @ComponentScan annotation.
6. What does the @SpringBootApplication annotation do
internally?
The @SpringBootApplication annotation is equivalent to using
@Configuration, @EnableAutoConfiguration, and @ComponentScan with
their default attributes. Spring Boot enables the developer to use a single
annotation instead of using multiple. But, as we know, Spring provided
loosely coupled features that we can use for each annotation as per our
project needs.
7. What is the purpose of using @ComponentScan in the
class files?
Spring Boot application scans all the beans and package declarations
when the application initializes. You need to add the @ComponentScan
annotation for your class file to scan your components added to your
project.
8. How does a spring boot application get started?
Just like any other Java program, a Spring Boot application must have a
main method. This method serves as an entry point, which invokes the
SpringApplication#run method to bootstrap the application.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
// other statements
}
}
9. What are starter dependencies?
Spring boot starter is a maven template that contains a collection of all
the relevant transitive dependencies that are needed to start a particular
functionality.
Like we need to import spring-boot-starter-web dependency for creating
a web application.
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
10. What is Spring Initializer?
Spring Initializer is a web application that helps you to create an initial
spring boot project structure and provides a maven or gradle file to build
your code. It solves the problem of setting up a framework when you are
starting a project from scratch.
11. What is Spring Boot CLI and what are its benefits?
Spring Boot CLI is a command-line interface that allows you to create a
spring-based java application using Groovy.
Example: You don’t need to create getter and setter method or access
modifier, return statement. If you use the JDBC template, it automatically
loads for you
Advanced Spring Boot Questions
13. What Are the Basic Annotations that Spring Boot
Offers?
The primary annotations that Spring Boot offers reside in
its org.springframework.boot.autoconfigure and its sub-packages. Here are
a couple of basic ones:
@EnableAutoConfiguration – to make Spring Boot look for auto-
configuration beans on its classpath and automatically apply them.
@SpringBootApplication – used to denote the main class of a Boot
Application. This annotation combines @Configuration,
@EnableAutoConfiguration, and @ComponentScan annotations with their
default attributes.
14. What is Spring Boot dependency management?
Spring Boot dependency management is used to manage dependencies
and configuration automatically without you specifying the version for
any of that dependencies.
15. Can we create a non-web application in Spring Boot?
Yes, we can create a non-web application by removing the web
dependencies from the classpath along with changing the way Spring
Boot creates the application context.
16. Is it possible to change the port of the embedded
Tomcat server in Spring Boot?
Yes, it is possible. By using the server.port in
the application.properties.
17. What is the default port of tomcat in spring boot?
The default port of the tomcat server-id 8080. It can be changed by
adding sever.port properties in the application.property file.
18. Can we override or replace the Embedded tomcat
server in Spring Boot?
Yes, we can replace the Embedded Tomcat server with any server by
using the Starter dependency in the pom.xml file. Like you can use
spring-boot-starter-jetty as a dependency for using a jetty server in your
project.
19. Can we disable the default web server in the Spring
boot application?
Yes, we can use application.properties to configure the web
application type i.e spring.main.web-application-type=none.
20. How to disable a specific auto-configuration class?
You can use exclude attribute of @EnableAutoConfiguration if you want
auto-configuration not to apply to any specific class.
//use of exclude
@EnableAutoConfiguration(exclude={className})
21. Explain @RestController annotation in Sprint boot?
It is a combination of @Controller and @ResponseBody, used for creating
a restful controller. It converts the response to JSON or XML. It ensures
that data returned by each method will be written straight into the
response body instead of returning a template.
22. What is the difference between @RestController and
@Controller in Spring Boot?
@Controller Map of the model object to view or template and make it
human readable but @RestController simply returns the object and
object data is directly written in HTTP response as JSON or XML.
23. Describe the flow of HTTPS requests through the
Spring Boot application?
On a high-level spring boot application follow the MVC pattern which is
depicted in the below flow diagram.
Spring Boot Flow Architecture
24. What is the difference between RequestMapping and
GetMapping?
RequestMapping can be used with GET, POST, PUT, and many other
request methods using the method attribute on the annotation. Whereas
getMapping is only an extension of RequestMapping which helps you to
improve on clarity on request.
25. What is the use of Profiles in spring boot?
While developing the application we deal with multiple environments
such as dev, QA, Prod, and each environment requires a different
configuration. For eg., we might be using an embedded H2 database for
dev but for prod, we might have proprietary Oracle or DB2. Even if DBMS
is the same across the environment, the URLs will be different.
To make this easy and clean, Spring has the provision of Profiles to keep
the separate configuration of environments.
26. What is Spring Actuator? What are its advantages?
An actuator is an additional feature of Spring that helps you to monitor
and manage your application when you push it to production. These
actuators include auditing, health, CPU usage, HTTP hits, and metric
gathering, and many more that are automatically applied to your
application.
27. How to enable Actuator in Spring boot application?
To enable the spring actuator feature, we need to add the dependency of
“spring-boot-starter-actuator” in pom.xml.
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator </artifactId>
</dependency>
28. What are the actuator-provided endpoints used for
monitoring the Spring boot application?
Actuators provide below pre-defined endpoints to monitor our application
-
Health
Info
Beans
Mappings
Configprops
Httptrace
Heapdump
Threaddump
Shutdown
29. How to get the list of all the beans in your Spring boot
application?
Spring Boot actuator “/Beans” is used to get the list of all the spring
beans in your application.
30. How to check the environment properties in your
Spring boot application?
Spring Boot actuator “/env” returns the list of all the environment
properties of running the spring boot application.
31. How to enable debugging log in the spring boot
application?
Debugging logs can be enabled in three ways -
We can start the application with --debug switch.
We can set the logging.level.root=debug property in application.property
file.
We can set the logging level of the root logger to debug in the supplied
logging configuration file.
32. Where do we define properties in the Spring Boot
application?
You can define both application and Spring boot-related properties into a
file called application.properties. You can create this file manually or use
Spring Initializer to create this file. You don’t need to do any special
configuration to instruct Spring Boot to load this file, If it exists in
classpath then spring boot automatically loads it and configure itself and
the application code accordingly.
33. What is dependency Injection?
The process of injecting dependent bean objects into target bean objects
is called dependency injection.
Setter Injection: The IOC container will inject the dependent bean object
into the target bean object by calling the setter method.
Constructor Injection: The IOC container will inject the dependent bean
object into the target bean object by calling the target bean constructor.
Field Injection: The IOC container will inject the dependent bean object
into the target bean object by Reflection API.
34. What is an IOC container?
IoC Container is a framework for implementing automatic dependency
injection. It manages object creation and its life-time and also injects
dependencies into the class.
What is the purpose of using @ComponentScan in the
class files?
Spring Boot application scans all the beans and package declarations
when the application initializes. You need to add the @ComponentScan
annotation for your class file to scan your components added to your
project
What does the @SpringBootApplication annotation do
internally?
The @SpringBootApplication annotation is equivalent to using
@Configuration, @EnableAutoConfiguration, and @ComponentScan with
their default attributes. Spring Boot enables the developer to use a single
annotation instead of using multiple. But, as we know, Spring provided
loosely coupled features that we can use for each annotation as per our
project needs
What is the starter dependency of the Spring boot
module?
Spring boot provides numbers of starter dependency, here are the most
commonly used -
Data JPA starter.
Test Starter.
Security starter.
Web starter.
Mail starter.
Thymeleaf starter.
What are starter dependencies?
Spring boot starter is a maven template that contains a collection of all
the relevant transitive dependencies that are needed to start a particular
functionality.
Like we need to import spring-boot-starter-web dependency for creating
a web application.
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
10. What is Spring Initializer?
Spring Initializer is a web application that helps you to create an initial
spring boot project structure and provides a maven or gradle file to build
your code. It solves the problem of setting up a framework when you are
starting a project from scratch.
JAVA Questions
1. What is Java?
2. Define Object in Java?
3. What is typecasting?
4. How many types of operators are available in Java?
5. What are the bitwise operators in Java?
6. List out the control statements in Java?
7. Describe in brief OOPs concepts?
8. What is a static variable?
9. What is the usage of this keyword in Java?
10. Can we have virtual functions in Java?
11. How many types of operators are available in Java?
12. What is the significant difference between JVM, JRE,
and JDK?
13. What are primitive data types?
14. What is Typecasting?
15. What are Java keywords?
Reference
https://www.simplilearn.com/tutorials/java-tutorial/java-interview-
questions
https://www.digitalocean.com/community/tutorials/java-string-
interview-questions-and-answers
https://www.codingninjas.com/codestudio/library/30-java-string-
interview-questions
https://www.edureka.co/blog/interview-questions/java-collections-
interview-questions/