In this post you will learn mostly asked springboot interview questions and answers. Nowadays interview expects more knowledge of Spring Boot from candidates. Spring Boot aims to simplify Java development with Spring by removing major pain points with respect to configuration, dependency management and ease of development.
In order to answer Spring Boot question with confidence, you not only know what problem Spring Boot solves but also in-depth knowledge of some of its core features like auto-configuration and starter dependencies. These two features eliminate a lot of configuration and setup work from Spring-based Java application.
1. What is springboot?
It is a framework for rapid application development built on top of the Spring Framework. We can say in simple words springboot is the combination of spring framework and embedded servers. It is used to create a stand-alone Spring-based application that you can just run because it needs minimal Spring configuration. In Spring Boot, there is no requirement for XML configuration (deployment descriptor) that means it decreases the effort of the developer.
2. Why Springboot is used?
To design any spring based application, a developer needs to be taken care on manual set up on Hibernate data source, Entity Manager, Session Factory, Transaction Management everything.
To Design all those common set up, a developer doesn’t need to define everything individually, SpringBootConfiguration annotation enough to manage everything at the time of deployment.
Spring MVC application some of the XML configuration mandatory to manage. Whereas Configuring Spring Boot Application nothing needs to be managed, the only annotation managed everything.
As configuration can be easily handled by manually, so Spring or Spring MVC can manage to not loading some of the unwanted default features for that specific application. In case of Spring Boot, it automatically handled on default loading part, so the developer doesn’t have as such concept of not loading some of the specific unusable spring default features.
3. what is use of @SpringBootApplication annotation and how it works 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 individual annotation as per our project needs.
4. What are starter dependencies?
Spring Boot starters are maven templates that contain a collection of all the relevant transitive dependencies that are needed to start a particular functionality.
For example, If we want to create a Spring WebMVC application then in a traditional setup, we would have included all required dependencies ourselves. It leaves the chances of version conflict which ultimately result in more runtime exceptions.
With Spring boot, to create web MVC application, all we need to import is spring-boot-starter-web dependency. Transitively, it brings in all other required dependencies to build a web application e.g. spring-webmvc, spring-web, hibernate-validator, tomcat-embed-core, tomcat-embed-el, tomcat-embed-websocket, jackson-databind, jackson-datatype-jdk8, jackson-datatype-jsr310 and jackson-module-parameter-names.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
5. What is Spring Initializer? why should you use it?
One of the difficult things to start with a framework is initial setup, particularly if you are starting from scratch and you don't have a reference setup or project. Spring Initializer addresses this problem in Spring Boot.
It's nothing but a web application which helps you to create initial Spring boot project structure and provides Maven or Gradle build file to build your code.
6. What is Spring Actuator? What are its advantages?
This is an interesting Spring Boot question and mostly asked on Java roles which also has some support responsibility. Spring Actuator is another cool Spring Boot feature which allows seeing inside a running application.
It allows you to see inside an application. Since Spring Boot is all about auto-configuration it makes debugging difficult and at some point in time, you want to know which beans are created in Spring's Application Context and how Controllers are mapped. Spring Actuator provides all that information.
It provides several endpoints e.g. a REST endpoint to retrieve this kind of information over the web. It also provides a lot of insight and metrics about application health e.g. CPU and memory usage, number of threads etc.
It also comes with a remote shell which you can use to securely go inside Spring Boot application and run some command to expose the same set of data. You can even use JMX to control this behavior at runtime.
Btw, it's important to secure your Spring Actuator endpoints because it exposes a lot of confidential information and a potentially dangerous one-two. For example, by using /showdown endpoint you can kill a Spring Boot application.
7. What is Spring Boot CLI? What are its benefits?
Spring Boot CLI is a command line interface which allows you to create Spring-based Java application using Groovy. Since it's used Groovy, it allows you to create Spring Boot application from the command line without ceremony e.g. you don't need to define getter and setter method, or access modifiers, return statements etc.
8. What are some common Spring Boot annotations?
Some of the most common Spring Boot annotations are @EnableAutoConfiguration, @SpringBootApplication, @SpringBootConfiguration, and @SpringBootTest.
The @EnableAutoConfiguration is used to enable auto-configuration on Spring Boot application, while @SpringBootApplication is used on the Main class to allow it to run a JAR file. @SpringBootTest is used to run unit test on Spring Boot environment.
9. What is the difference between @SpringBootApplication and @EnableAutoConfiguration annotation?
Even though both are essential Spring Boot application and used in the Main class or Bootstrap class there is a subtle difference between them. The @EnableAutoConfiguration is used to enable auto-configuration but @SpringBootApplication does a lot more than that.
It also combines @Configuration and @ComponentScan annotations to enable Java-based configuration and component scanning in your project.
The @SpringBootApplication is in fact combination of @Configuration, @ComponentScan and @EnableAutoConfiguration annotations
It's also very powerful and can auto-include a lot of library in Groovy's default package if you happen to use it.
For example, if you use JdbcTempalte, it can automatically load that for you
10. Can we disable the default web server in the Spring Boot application?
The major strong point in Spring is to provide flexibility to build your application loosely coupled. Spring provides features to disable the web server in a quick configuration. Yes, we can use the application.properties to configure the web application type, i.e. spring.main.web-application-type=none
11.Can we override or replace the Embedded Tomcat server in Spring Boot?
Yes, we can replace the Embedded Tomcat with any other servers by using the Starter dependencies. You can use spring-boot-starter-jetty or spring-boot-starter-undertow as a dependency for each project as you need.
12. How to do unit testing and integration testing in a spring boot application?
Typically any software application is divided into different modules and components. When one such component is tested in isolation, it is called unit testing.
Unit tests do not verify whether the application code works with external dependencies correctly. It focuses on single component and mocks all dependencies this component interacts with.
To perform unit testing, we can take help of specialized annotations such as :
• @JdbcTest – 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 – is used to test REST clients.
• @WebMvcTest – used for Spring MVC tests with configuration relevant to only MVC tests.
• In integration testing, we shall focus on testing complete request processing from controller to persistence layer.
• The @SpringBootTest annotation helps in writing integration tests. It starts the embedded server and fully initializes the application context. We can inject the dependencies in test class using @Autowired annotation.
• We can also provide test specific beans configuration using nested @Configuration class or explicit @TestConfiguration classes.
13. How to enable debug logging?
To enable debug logging,
1. we can start the application with the --debug switch.
2. we can set the logging.level.root=debug property in application.properties file.
3. We can set the logging level of root logger in supplied logging configuration file.
14. How to enable HTTPS/SSL support in Spring boot?
The SSL support in spring boot project can be added via application.properties and by adding the below entries.
application.properties
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
15.Is this possible to change the port of Embedded Tomcat server in Spring boot?
Yes, it's possible to change the port. You can use the application.properties file to change the port. But you need to mention "server.port" (i.e. server.port=8081). Make sure you have application.properties in your project classpath; REST Spring framework will take care of the rest. If you mention server.port=0 , then it will automatically assign any available port.
16. Where do you define properties in 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 you can use Spring Initializer to create this file, albeit empty.
You don't need to do any special configuration to instruct Spring Boot load this file. If it exists in classpath then Spring Boot automatically loads it and configure itself and application code according.
No comments:
Post a Comment