Spring Boot Interview Questions and Answers for freshers & Experienced.
Spring Boot is an open source and light-weight framework for development of Java based Applications. It is built on the top of the Spring framework. It is developed and maintained by Pivotal team. It is mainly used for Web and command line applications. It configures all the features automatically and you can run the application with one click. Spring framework is the most widely used Java Framework for building Applications. the main feature of spring framework is dependency injection. It helps make things simpler by allowing us to develop loosely coupled applications. Spring Boot is a Module of Spring framework. The main feature of Spring Boot is Autoconfiguration. It automatically configures a class based on that requirement. Spring Boot makes it easy to crate stand lone spring-based application that you run. The main difference is: spring framework used several configurations for development applications. Spring Boot allows for automatic configurations using predefined class paths. Java latest version Spring Boot 2.X (above) will no longer to support Java 7 or below versions, Being Java 8 is the minimum requirement. Build tools: Embedded servlet containers: Spring Boot provides a number of starter dependencies for Spring modules. Here, some of the most commonly used one are: For More: Actuator is a sub project of Spring Boot. It provides several endpoints in order to monitor and manage with your application. It does so by providing built-in endpoints, but you are also able to build your own endpoints. Also provides easy way to access the production ready REST points and fetch all the information from the web. Actuator make it very easy to integrate your application with any external system with a very minimal configuration. Check the following maven dependency in your existing POM.XML file: Spring Boot CLI is a Command line interface tool for spring Boot application. It uses Groovy Script.It is a powerful tool for create /manger your applications. Some common Spring Boot Annotations are: @SpringBootApplication @importAutoConfiguration @AutoConfigureBefore or After or Order Conditional Annotations /beans: this endpoint returns a complete list of all the spring beans in your application /dump: It performs a thread dump /env: This endpoint returns list of properties in current environment /health: For your application health information /trace: for trace logs /info: displays arbitrary application information /auditevents: Audit events information from the current application /mappings: It display the list of all @RequestMapping paths. /metrics: shows the metrics information-e.g: JVM,system CPU,openfiles Spring Boot uses some relaxed rules for binding environment properties to @configuartionproperties beans, so there does not need to be an exact match between the environment property name and the bean property name. In Spring Boot, first you create a folder called static under resources folder. You can put your static content in that folder. For example, the path to interviewgigapp.js would be resources\static\js\interviewgigapp.js You can refer it your code with Autoconfiguration is an important feature to automatically configure an application based on dependencies that are present on the class path as jars, beans, properties, etc., requiring no efforts from the developer. Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. If we build a project with Maven, that file should be placed in the resources/META-INF directory, which will end up in the mentioned location during the package phase. You can run spring boot application as a JAR file from the command prompt without setting up a web Server. But to run a WAR file, you need to setup a webserver like Tomcat, or underflow, or jetty which has servlet container and then you need to deploy WAR file there. It is Maven Plugin built by Spring Boot team to make packaging your applications easier. It provides a few commands which enable you to package the code as ajar or run the application. The plugin provides several goals to work with a Spring Boot application: spring-boot:repackage: create a jar or war file that is auto-executable. It can replace the regular artifact or can be attached to the build lifecycle with a separate classifier. spring-boot:run: run your Spring Boot application with several options to pass parameters to it. spring-boot:start and stop: integrate your Spring Boot application to the integration-test phase so that the application starts before it. spring-boot:build-info: generate a build information that can be used by the Actuator. It is a Spring Boot tool to Bootstrap Boot or Spring Applications very easily. A starter is just a Gradle or Maven module with the sole purpose of supplying all the necessary dependencies to “get started” with specific functionality. Spring Boot Starters make the bootstrapping process much easier and faster. The starter brings you required Maven dependencies as well as some predefined configuration bits. It is used to manage dependencies and configure automatically without you specifying the version for any of that dependencies. Spring upgrades all dependencies automatically in a consistent way when we update the spring boot version. Thymeleaf is one of the java-based libraries used to create web app. It provides a support for serving a XHTML/HTML5 in web apps. It is powerful template processing engine for the spring framework. It provides an ensemble of very useful tools that enhance the development experiences a lot. Such as automatic upon saving and much more. Spring Data JPA makes it easy to implement JPA based repositors and build spring powered applications that use data access technologies. H2 is an opensource RDBM (Rational Database Management) system created entirely in java. It can be embedded in java applications or run in the client server mode. Or It is a lightweight database that can be run in memory. @SpringBootApplication annotation is a Combination of following three spring annotations and provides the functionality of all three with just one line of code. @configuration or @springBootConfiguration (In version 2): It indicates that a class provides spring Boot application @configuration. @componentScan: It provides support parallel with Spring XMLs context:component-scan element. @enableAutoconfiguration: It is used to enable spring Boot’s auto configuration feature. we can control logging with Spring Boot by specifying log levels on application.properties file. Spring Boot loads this file when it exists in the classpath and it can be used to configure both Spring Boot and application code. Spring Boot uses Commons Logging for all internal logging and you can change log levels by adding following lines in the application.properties file: Check the steps that happen when you interact with a web page Enter the details Browser: Creates a POST request where details are put into the request (and encrypted if you are using https) Routers and Network: The POST request is sent through multiple routers before it reaches the destination In the case of a get request and post request, in steps a and b you can see the details on the browser. The real change is in step c. Hibernate Validator is a validation framework. It is not related to Database. It is the reference implementation Bean Validation 1.1! Actually, you can use any implementation of Bean Validation API. However, there are really no other popular options. Add below lines into your application.properties file: application.properties @SpringBootApplication Output 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 selected server. Follow the below steps: The SSL support in spring boot project can be added via application.properties and by adding the below entries. application.properties First, you have to add the following mapping in your pom.xml configuration file: CommandLineRunner will execute run() method, just after applicationcontext is created and before springboot application starts up. It accepts the argument, which are passed at time of server startup. @Component @Override If you want to create an executable jar in spring boot then you must be adding the following in your pom.xml file. pom.xml:- Use “mvn clean” package to create the executable jar file. The default H2 database is testdb. Refer below: spring.datasource.name=testdb # Name of the datasource. Spring DAO(Data Access Object) support in Spring makes it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. It supports three embedded containers: Tomcat (default server), Jetty, and Undertow. By default, it uses Tomcat as embedded containers but you can change it to Jetty or Undertow. There are two ways to add Servlet, Filter, ServletContextListener and the other listeners supported by the Servlet spec to your application. You can either provide Spring beans for them or enable scanning for Servlet components. It supports Log4j 2 for logging configuration if it is on the classpath. If you are using the starters for assembling dependencies that means you have to exclude Log back and then include log4j 2 instead. Spring Batch auto-configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default, it executes all Jobs in the application context on start-up. Spring Boot uses some relaxed rules for resolving configuration properties name such that a simple property name can be written in multiple ways. For example, a simple property “log.level.our-package” can be written in following ways and all are correct and will be resolved by framework for its value based on property source. Some relaxed binding rules per property source: @JdbcTest : It can be used for a typical JDBC test when a test focuses only on JDBC-based components. @JsonTest : It is used when a test focuses only on JSON serialization. @RestClientTest : It is used to test REST clients. It auto-configures different dependencies such as Jackson, GSON, and Jsonb support; configures a RestTemplateBuilder; and adds support for MockRestServiceServer by default. @WebMvcTest : It used for Spring MVC tests with configuration relevant to only MVC tests. First, start the application with the –debug switch. Next, set the logging.level.root=debug property in application.properties file. Final,Set the logging level of root logger in supplied logging configuration file.What is Spring Boot?
What is the difference between spring Framework and Spring Boot?
What are the features of Spring Boot?
What are the minimum system requirements for Spring Boot?
What are the Spring Boot Key Components?
What are some of the starter dependencies for spring modules?
What is a Spring Boot Actuator?
How to enable spring Boot actuator?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
What is Spring Boot CLI?
List the Spring Boot CLI commands?
What are the common Spring Boot Annotations?
What are the most used Spring Actuator End Points?
What is use of Relaxed Binding in Spring Boot?
Name some of the spring sub projects?
How to add custom JS code in Spring Boot?
<script src="/js/interviewgigapp.js"></script>
Can you explain Auto Configuration in Spring Boot?
Can you explain, how to Register a Custom Auto-Configuration?
To register an auto-configuration class, we must have its fully-qualified name listed under the EnableAutoConfiguration key in the META-INF/spring.factories file:=
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration
What is the difference between an embedded container and a WAR?
Why we use Spring Boot Maven plugin?
Explain, how to enable HTTP/2 support in Spring Boot?
Adding below property to your properties (or. yml) file:
Server.http/2.enabled=true.
What is Spring Boot Initilizr?
What are the Spring Boot starters?
What are the Pros of Spring Boot?
What is use of Spring Boot dependency management?
What is use of Thymeleaf?
What is the use of Spring Boot Devtools?
What is use of a Spring Data JPA?
What is H2 in Spring Boot?
What are @SpringBootApplication annotations?
Explain, how can you control logging with Spring Boot?
logging.level.org.springframework=DEBUG
logging.level.com.demo=INFO
Why Spring Boot?
Can you explain why is request method POST recommended compared to GET for sensitive data?
Can you explain why do we use Hibernate validator?
Can you explain how to configure Spring Boot to show Hibernate SQL Query in logs?
#this line shows the sql statement in the logs
logging.level.org.hibernate.SQL=debug
#this line shows sql values in the logs
logging.level.org.hibernate.type.descriptor.sql=trace
How to replacing default converters (HttpMessageConverters)
public class AppMain {
@Bean
public HttpMessageConverters converters() {
return new HttpMessageConverters(
false, Arrays.asList(new TheCustomConverter()));
}
}
[email protected]
Can you explain, how to deploy to a different server with Spring Boot?
Can you explain, how to connect an external database like MySQL or Oracle?
Can you explain, how to enable HTTPS/SSL support in Spring boot?
server.port=8443
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=classpath:ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
How can you enable auto reload of application with Spring Boot?
<-- Spring Boot DevTools for the developer friendly options like Auto Restart of the server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Can you explain CommandLineRunner in Spring Boot?
public class CommandLineAppStartupRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
public void run(String...args) throws Exception {
logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args));
}
}
How to create an executable jar using spring boot?
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
What is the default H2 database configured by Spring Boot?
What is Spring DAO support?
What embedded containers does Spring Boot support?
How do you add a Servlet, Filter or Listener to an application?
Can you explain, how do you Configure Log4j for logging?
Can you explain, how to execute Spring Batch jobs on start-up?
Can you explain relaxed binding?
Which annotations are used to perform Unit testing in Spring Boot?
Can you explain how to enable debug logging?
Can you explain,how do you run and stop spring boot executable jar?
Open cmd or shell window and use java -jar as shown below
java -jar my project-0.0.1-SNAPSHOT.jar
To stop use ctrl+C
Related posts:
- Core Java Interview Questions and Answers
- Hibernate Interview Questions and Answers 2021
- Java Multithreading Interview Question and Answers
- JBOSS Interview Questions and Answers
- JDBC Interview Questions and Answers
- JNDI Interview Questions and Answers
- Portal and Portlet Interview Questions and Answers
- Spring Interview Questions and Answers