Java Springboot Developer
Java Springboot Developer
Sprint boot is a Java-based spring framework used for Rapid Application Development (to build stand-
alone microservices). It has extra support of auto-configuration and embedded application server like
tomcat, jetty, etc.
Because of its fast, low configuration, inbuild server, and monitoring features, it helps to build a stand-
alone java application from scratch with very robust and maintainable.
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
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.
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 {
SpringApplication.run(MyApplication.class);
// other statements
}
}
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>
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.
12. What are the most common Spring Boot CLI commands?
-run, -test, -grap, -jar, -war, -install, -uninstall, --init, -shell, -help.
13. What Are the Basic Annotations that Spring Boot Offers?
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?
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?
//use of exclude
@EnableAutoConfiguration(exclude={className})
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.
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.
consumes = “text/plain”
consumes = {“text/plain”, “application/*”}
24. 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.
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.
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>
27. What are the actuator-provided endpoints used for monitoring the Spring boot
application?
Health
Info
Beans
Mappings
Configprops
Httptrace
Heapdump
Threaddump
Shutdown
28. 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.
29. 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.
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.
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.
33. What is an IOC container?
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.
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.
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
Java 8 +
Spring Framework 5.1.9 +
Maven 3.3+
Gradle 4.4+
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.
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
o 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:
CREATE TABLE customers(customerid INT PRIMARY KEY NOT NULL AUTO_INCREMENT, customername
1
VARCHAR(255));
Step 3: Now, create a Spring Boot project and provide the required details
Step 5: Once the project is created, you have to configure the database into application properties
1 spring.datasource.url=jdbc:mysql://localhost:3306/example
2 spring.datasource.username=root
3 spring.datasource.password=edureka
4 spring.jpa.hibernate.ddl-auto=create-drop
Step 6: The main application.java class should have the following code:
1 package com.edureka;
2 import org.springframework.boot.SpringApplication;
3 import org.springframework.boot.autoconfigure.SpringBootApplication;
4 @SpringBootApplication
7 SpringApplication.run(SampleApplication.class, args);
8 }
9 }
Step 7: Next, you have to create a controller to handle the HTTP requests, by mentioning the following
code:
1 package com.edureka;
2 import org.springframework.web.bind.annotation.RequestMapping;
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.jdbc.core.JdbcTemplate;
5 import org.springframework.web.bind.annotation.RestController;
6 @RestController
8 @Autowired
9 JdbcTemplate jdbc;
10 @RequestMapping("/insert")
14 }
15 }
To use this annotation, you have to To use this annotation, you have to
import org.springframework.web. import org.springframework.web.
bind.annotation.RequestMapping; bind.annotation.RestController;
Example: Consider you have a method example() which should map with /example URL.
1 package com.edureka;
2 import org.springframework.web.bind.annotation.RequestMapping;
3 import org.springframework.web.bind.annotation.RestController;
4 @RestController
6 @RequestMapping("/example")
8 return"Welcome To Edureka";
9 }
10 }
Q19. What is Spring Boot CLI and how to execute the Spring Boot project using
boot CLI?
Spring Boot CLI is a tool supported by the official Spring Framework. The steps to execute a Spring Boot
project are as follows:
Download the CLI tool from the official site and extract the zip file. The bin folder present in the
Spring setup is used to execute the Spring Boot application.
Since Spring Boot CLI executes groovy files, you need to create a groovy file for Spring Boot
application. So, to do that, open terminal and change the current directory to the bin folder. Now,
open a groovy file (for example Sample.groovy)
In this file create a controller as follows:
Once, the project is executed go to the URL(localhost:8080:/example) and you will see the output
as Welcome To Edureka
In case you are facing any challenges with these Spring Boot interview questions, please comment on
your problems in the comment section below.
Auto-configuration is used to automatically configure the required configuration for the application. For
example, if you have a data source bean present in the classpath of the application, then it automatically
configures the JDBC template. With the help of auto-configuration, you can create a Java application in an
easy way, as it automatically configures the required beans, controllers, etc.
1 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
If the class is not on the classpath, then to exclude the auto-configuration, you have to mention the
following code:
1 @EnableAutoConfiguration(excludeName={Sample.class})
Apart from this, Spring Boot also provides the facility to exclude list of auto-configuration classes by using
the spring.autoconfigure.exclude property. You can go forward, and add it either in the
application.properties or add multiple classes with comma-separated.
It is a combination of @Configuration,
It is a combination of @Configuration
@ComponentScan and
and @ComponentScan annotations
@EnableAutoConfiguration annotations.
Q25. What are the steps to deploy Spring Boot web applications as JAR and WAR
files?
To deploy a Spring Boot web application, you just have to add the following plugin in the pom.xml file:
1 <plugin>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-maven-plugin</artifactId>
4 </plugin>
By using the above plugin, you will get a JAR executing the package phase. This JAR will contain all the
necessary libraries and dependencies required. It will also contain an embedded server. So, you can
basically run the application like an ordinary JAR file.
Note: The packaging element in the pom.xml file must be set to jar to build a JAR file as below:
1 <packaging>jar</packaging>
Similarly, if you want to build a WAR file, then you will mention
1 <packaging>war</packaging>
Q.26 Can you give an example for ReadOnly as true in Transaction management?
Consider a scenario, where you have to read data from the database. For example, let us say you have a
customer database, and you want to read the customer details such as customerID, and customername.
To do that, you will set read-only on the transaction as we do not want to check for the changes in the
entities.
Q27. Can you explain how to deploy to a different server with Spring Boot?
To deploy a different server with Spring Boot, follow the below steps:
Note: The steps to deploy the WAR file on the server is dependent on the server you choose.
Q28: What is the best way to expose custom application configuration with Spring
Boot?
One way to expose the custom application configuration in Spring Boot is by using the @Value
annotation. But, the only problem with this annotation is that all the configuration values will be
distributed throughout the application. Instead, you can use a centralized approach.
By centralized approach, I mean that you can define a configuration component using the
@ConfigurationProperties as follows:
1 @Component
2 @ConfigurationProperties("example")
According to the above snippet, the values configured in application.properties will be as follows:
1 example.number: 100
2 example.value: true
Q 30. What are the steps to connect an external database like MySQL or Oracle?
To connect an external database, you have to follow the below steps:
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:
Now, to expose the REST services, you can use the POST method in the following way:
1 {
2 "customername": "Rohit"
3 }
Response Content
1 {
2 "customername": "Rohit"
3 "_links": {
4 "self": {
5 "href": "http://localhost:8080/sample/1"
6 },
7 "sample": {
8 "href": "http://localhost:8080/sample/1"
9 }
10 }
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/*”}
3 PagingAndSortingRepository<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.
WAR benefits a considerable measure from Spring Only one component of Spring Boot and is utilized
Boot during improvements
Now, let’s say, you want to have different application configuration in each of the environments, you can
use profiles to have different application configurations for different environments. So, basically, Spring
and Spring Boot provide features through which you can specify:
Q41. What are the steps to add a custom JS code with Spring Boot?
The steps to add a custom JS code with Spring Boot are as follows:
Now, create a folder and name it static under the resources folder
In this folder, you can put the static content in that folder
Note: Just in case, the browser throws an unauthorized error, you either disable the security or search for
the password in the log file, and eventually pass it in the request header.
Cannot determine embedded database driver class for database type NONE
To resolve this error, add H2 to the pom.xml file, and restart your server.
The following code snippet can be added to add the dependency:
1 <dependency>
2 <groupId>com.h2database</groupId>
3 <artifactId>h2</artifactId>
4 <scope>runtime</scope>
5 </dependency>
dev
prod
Consider the following properties present in the application properties file:
example.number: 100
example.value: true
example.message: Dynamic Message
Now, say you want to customize the application.properties for dev profile, then you need to create a file
with name application-dev.properties and override the properties that you want to customize. You can
mention the following code:
Q46. Mention the dependencies needed to start up a JPA Application and connect
to in-memory database H2 with Spring Boot?
The dependencies are needed to start up a JPA Application and connect to in-memory database H2 with
Spring Boot
web starter
h2
data JPA starter
To include the dependencies refer to the following code:
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>com.h2database</groupId>
7 <artifactId>h2</artifactId>
8 <scope>runtime</scope>
9 </dependency>
10 <dependency>
11 <groupId>org.springframework.boot</groupId>
12 <artifactId>spring-boot-starter-data-jpa</artifactId>
13 </dependency>
sampleProp
sample-Prop
sample_Prop
SAMPLE_PROP
Q48. Where is the database connection information specified and how does it
automatically connect to H2?
Well, the answer to this question is very simple. It is because of the Spring Boot auto-configuration that,
configures the dependencies of the application. So, the database connection information, and
automatically connecting the database to H2 is done by the auto-configuration property.
Q49. What is the name of the default H2 database configured by Spring Boot?
The name of the default H2 database is testdb. Refer below:
Q50. Do you think, you can use jetty instead of tomcat in spring-boot-starter-web?
Yes, we can use jetty instead of tomcat in spring-boot-starter-web, by removing the existing dependency
and including the following:
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-web</artifactId>
4 <exclusions>
5 <exclusion>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-tomcat</artifactId>
8 </exclusion>
9 </exclusions>
10 </dependency>
11 <dependency>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-jetty</artifactId>
14 </dependency>