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:
<packaging>jar</packaging>
Similarly, if you want to build a WAR file, then you will mention
<packaging>war</packaging>
Q.26 Can you give an example for ReadOnly as true in Transaction management?
Example for ReadOnly as TRUE in transaction management could be as follows:
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:
- Generate a WAR from the project
- Then, deploy the WAR file onto your favorite server
Note: The steps to deploy the WAR file on the server is dependent on the server you choose.
Want to upskill yourself to get ahead in your career? Check out this video
Top 10 Technologies to Learn in 2024 | Edureka
This Edureka video on 𝐓𝐨𝐩 𝟏𝟎 𝐓𝐞𝐜𝐡𝐧𝐨𝐥𝐨𝐠𝐢𝐞𝐬 𝐭𝐨 𝐋𝐞𝐚𝐫𝐧 𝐢𝐧 𝟐𝟎𝟐4 will introduce you to all the popular and trending technologies in the market which you should focus on in 2024. These are the trending technologies that you need to learn in order to have a successful career in the year 2024.
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:
@Component
@ConfigurationProperties("example")
public class SampleConfiguration {
private int number;
private boolean value;
private String message;
According to the above snippet, the values configured in application.properties will be as follows:
example.number: 100
example.value: true
example.message: Dynamic Message
Q29. 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.
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:
- Start by adding the dependency for MySQL Connector to pom.xml
- Then remove H2 Dependency from pom.xml
- Now, set up your MySQL database and configure your connection to the MySQL database
- Restart your project
What are the Spring Boot key components?
Here are the key components of Spring Boot:
- Auto-configuration: One of the most attractive features of Spring Boot is its ability to automatically configure your application based on the dependencies you’ve added to your project. For example, if you have a MySQL database driver on your classpath, Spring Boot auto-configures a DataSource.
- Starter Dependencies: Spring Boot provides a set of starter projects to simplify your build configuration. These starters bring in all the dependencies you’ll need for a specific type of application. For instance, if you are developing a web application, you can add the spring-boot-starter-web dependency to your project, and it will include all necessary dependencies.
- Embedded Servers: Spring Boot has embedded Tomcat, Jetty, and Undertow servers, meaning you don’t need to deploy WAR files. Your Spring Boot application can be a standalone application with an embedded server.
- Actuator: The Spring Boot Actuator module provides production-ready features to help you monitor and manage your application. It includes features like health checks, metrics gathering, HTTP tracing, etc.
- CLI (Command Line Interface): This is another key component of Spring Boot, which is optional. The CLI allows you to develop Spring Boot applications using Groovy. It simplifies the code structure by automatically adding all the necessary annotations and imports.
Why Spring Boot over Spring?
Due to the following reasons:
- Simplicity & Speed: Spring Boot simplifies the bootstrapping and development process of Spring applications. It’s easier and quicker to set up a new Spring application with Spring Boot, which makes it ideal for beginners and for situations where speed of development is important.
- Auto-configuration: Spring Boot offers ‘auto-configuration’ which takes the guesswork out of configuring Spring applications. It can automatically provide configuration properties based on what it sees on your application’s classpath. For example, if Spring Boot detects HSQLDB on your classpath, it will automatically configure an in-memory database for you.
- Embedded Servers: Spring Boot applications can include an embedded servlet container (like Tomcat, Jetty, or Undertow), allowing them to be packaged as standalone executable JARs. This is very convenient for microservice architectures and for cloud-based deployment, as there is no need for external servlet containers.
What is the starter dependency of the Spring boot module?
Here are some of the commonly used Spring Boot Starter dependencies:
- spring-boot-starter-web: It is used for building web, including RESTful, applications using Spring MVC. It uses Tomcat as the default embedded container.
- spring-boot-starter-data-jpa: It simplifies the development of Spring applications that use data access technologies, relational databases, and distributed databases providing powerful capabilities such as Spring Data JPA, Hibernate, DataSource setup, and others.
- spring-boot-starter-security: It is used for Spring Security. It is a powerful and customizable authentication and access-control framework.
- spring-boot-starter-test: It is used for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito.
- spring-boot-starter-data-rest: It is used to expose simple RESTful services using Spring Data REST.
What does the @SpringBootApplication annotation do internally?
The @SpringBootApplication annotation is a convenience annotation in Spring Boot that adds all of the following:
- @Configuration: Designates this class as a configuration class. Configuration classes are the heart of Java-based application configuration in Spring. They can use @Bean annotated methods to specify bean definitions.
- @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration feature, which attempts to automatically configure your application based on the dependencies in its classpath. For example, if Spring MVC is on the classpath, this annotation flags the application to be web-applicable and activates key behaviors like setting up a DispatcherServlet.
- @ComponentScan: Enables component scanning. This allows Spring to automatically discover other components, configurations and services in the same package as the one where the @SpringBootApplication is placed, allowing it to automatically manage them (i.e., create bean instances for your classes at application startup).
What is the purpose of using @ComponentScan in the class files?
@ComponentScan is an annotation that is used with @Configuration to tell Spring the packages to scan for annotated components. Annotated components include other @Configuration classes, as well as @Component, @Service, @Repository, @Controller, and @RestController, among others.When Spring finds these components, it automatically registers the beans in the application context.
What is Spring Initializer ?
Spring Initializr is a web-based tool provided by the Spring team, which allows users to quickly bootstrap a Spring Boot application. It’s designed to help you start a new Spring Boot project within seconds.
You can access Spring Initializr through the following URL: https://start.spring.io/. Some IDEs, like IntelliJ IDEA and Spring Tools Suite (STS), have integrated support for Spring Initializr, which means you can create a new project via Initializr right from within the IDE.
What are the Spring Boot key components?
- Spring Boot Starters: Pre-configured sets of dependencies grouped together to simplify your build configuration.
- Autoconfiguration: Automatically configures Spring application based on the jar dependencies present on the classpath.
- Embedded Servers: Allows you to create standalone, self-contained Spring applications that can be run without needing to deploy on an external server.
- Spring Boot Actuator: Provides built-in production-ready features like health checks and metrics.
- Externalized Configuration: Allows the separation of configuration from code, enabling the same application to run in different environments.
- Spring Boot CLI: A command-line tool to quickly create and run Spring applications using Groovy scripts.
- Spring Boot Test: Provides utilities and annotations to simplify the testing of Spring applications.
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:
@RepositoryRestResource(collectionResourceRel = "sample", path = "sample")
public interface SampleRepository
extends CustomerRepository<sample, Long> {
Now, to expose the REST services, you can use the POST method in the following way:
{
"customername": "Rohit"
}
Response Content
{
"customername": "Rohit"
"_links": {
"self": {
"href": "http://localhost:8080/sample/1"
},
"sample": {
"href": "http://localhost:8080/sample/1"
}
}
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?
@RepositoryRestResource(collectionResourceRel = "sample", path = "sample")
public interface SampleRepository extends
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.
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 | Only one component of Spring Boot and is utilized during improvements |
Q40. What do you think is the need for Profiles?
Profiles are used to provide a way to segregate the different parts of the application configuration and make it available for various environments. So, basically, any @Component or a @Configuration can be marked with a @Profile to limit as it is loaded. Consider you have multiple environments,
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:
- The active profile for a specific environment
- The configuration of various environments for various profiles.
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.
Q42. How to instruct an auto-configuration to back off when a bean exists?
To instruct an auto-configuration class to back off when a bean exists, you have to use the @ConditionalOnMissingBean annotation. The attributes of this annotation are as follows:
- value: This attribute stores the type of beans to be checked
- name: This attribute stores the name of beans to be checked
Q43. Why is Spring Data REST not recommended in real-world applications?
Spring Data REST is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider is the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered. The entities are directly exposed. So, I would just say, you can use Spring Data REST, for the initial evolution of the project.
Q44. What is the error you see if H2 is not in the classpath?
If H2 is not present in the classpath, then you see the following error:
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:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Q45. What is the way to use profiles to configure the environment-specific configuration with Spring Boot?
Since it is a known fact that a Profile is nothing but a key to identify an environment lets consider the following two profiles in the example:
- 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:
example.message: Dynamic Message in Dev
Similarly, if you want to customize the application.properties for prod profile, then you can mention the following code snippet:
example.message: Dynamic Message in Prod
Once you are done with the profile-specific configuration, you have to set the active profile in an environment. To do that, either you can
- Use
-Dspring.profiles.active=prod
in arguments - Use
spring.profiles.active=prod
in application.properties file
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:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Q47. What do you understand by Spring Boot supports relaxed binding?
Relaxed binding, is a way in which, the property name does not need to match the key of the environment property. In Spring Boot, relaxed binding is applicable to the type-safe binding of the configuration properties. For example, if a property in a bean class with the @ConfigurationPropertie annotation is used sampleProp, then it can be bounded to any of the following environment properties:
- 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:
spring.datasource.name=testdb # Name of the datasource.
Note: Just incase if you are using H2 in-memory database, then exactly that is the name of Spring Boot which is used to setup your H2 database.
Spring Boot Interview Questions
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:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
With this, we come to an end to this article on Spring Boot Interview Questions. I hope this set of Spring Boot Interview Questions and Answers will help you in preparing for your interviews. All the best! If you want to learn Spring and wish to use it while developing Java applications, then check out the Spring Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of “Spring Boot Interview Questions” and we will get back to you.