Interview Questions On SpringBoot

Interview Questions On SpringBoot

·

5 min read

As I have already shared the interview questions on Java 8 and Spring Boot Annotation based questions. Now let us have a glance on the questions which can be asked specific to Spring Boot Application.

The below given questions are helpful for beginners as well as experienced people who are attending interview with their primary skills as Java, Spring and Microservice. If required bookmark this page as I will keep adding more questions to it.

  1. What is Spring Boot?

    Spring Boot is a Java based spring framework, it provides Rapid application development features like auto-configuration, embedded servers, package structures.

  2. Why Spring Boot over Spring?

    • Spring Boot provides a stand-alone application with minimal configuration.

    • It provides embedded tomcat, jetty servers. So we just have to write the code and run the application.

    • No XML configuration is required.

    • Provides production ready features such as health checks, actuators etc.

    • Provides In-memory database, version management, component scanning etc.

  3. How does Spring Boot works internally?

    • Spring Boot automatically configures the application based on the dependencies added during creation of the project

    • 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.

  4. How does @SpringBootApplication works internally?

  5. How to disable @EnableAutoConfiguration, included in @SpringBootApplication?

    • There are some requirements where we do not want to use some of the specific auto-configuration classes. We can disable them using specific attributes.

    • For this the exclude attribute of the @EnableAutoConfiguration annotation can be used.

        @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class) 
        public class ApplicationConfiguration { 
           ... 
        }
      
  6. How to disable embedded tomcat server and use other servers?

    • Spring boot by default provides tomcat and Jetty servers, if we do not want we can exclude these default servers.

    • To exclude tomcat from servers, we just have to add the additional block to the Spring Boot starter dependency.

    • We have to add <exclusions> tags that make sure the given artifact is removed at build time.

        <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>
      
  7. How to convert Spring Boot REST Application as Web Application?

    • As we know, by default spring boot provides embedded servers and makes the application production ready in terms of REST API's

    • To convert REST into Web application we have to disable the embedded servers and set the configuration to false in properties files.

      
        @SpringBootApplication(exclude = 
        {EmbeddedServletContainerAutoConfiguration.class, 
        WebMvcAutoConfiguration.class})
        public class ApplicationConfiguration { 
           ... 
        }
      
      • And need to add the below property to non-rest applications so that spring boot does not try to start the
        WebApplicationContext. This should go to the application.properties.

          spring.main.web-environment=false
        
  8. What is the default port of tomcat in spring boot? How to change the default port number?

    • The default port of the tomcat server-id 8080.

    • It can be changed by adding sever.port properties in the application.property file.

  9. Explain @RestController annotation of Spring Boot?

    • The @RestController annotation is a combination of @Controller and @ResponseBody, which is used for creating a restful api's.

    • It converts the response to JSON or XML.

    • It makes sure that data returned by each method will be written directly into the response body instead of returning a template.

  10. What is the difference between @RestController and @Controller in Spring Boot?

    • @Controller Map the model object to view or template and make it human readable.

    • @RestController simply returns the object and object data is directly written in HTTP response as JSON or XML.

  11. What are Spring Profiles?

    • When we are developing the application in the organizations, we deal with multiple environments such as -

      • dev - for development

      • QA - for test environment

      • Pre-Prod - for production environment

      • Prod - for live or production ready

    • So each environment requires different configurations.

    • For example: we might be using an embedded H2 database for dev but for prod - we might have 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.

  12. Describe the flow of HTTP request through the Spring Boot Application?

    • Spring Boot mainly be having 4 layers

      • Presentation Layer – Authentication & Json Translation

      • Business Layer – Business Logic, Validation & Authorization

      • Persistence Layer – Storage Logic

      • Database Layer – Actual Database

    • When the Client makes an HTTP request through GET, PUT, POST, etc. The HTTP request is forwarded to the Controller. The controller maps the request. It processes the handles and calls the server logic, i.e service class

    • The business logic is performed in the Service layer. The spring boot performs all the logic over the data of the database which is mapped to the spring boot model class through Java Persistence Library(JPA)/Hibernate.

What is the difference between application.properties file and application.yml file?

  • There are the files, where we mention in which port our application should run, what are the credentials required for db is mentioned in these

application.properties

application.yml

It stores data in sequential format

It stores data in hierarchical format.

It supports only key-value pairs (basically string values)

It supports key-value pair, as well as map, list & scalar type values.

It is specifically used by Java

It can be used by other languages (eg Java, Python, ROR, etc).

When managing multiple configuration profiles, then: .properties requires you to create .properties file per every profile

When managing multiple configuration profiles, then in - .yml you can create a section for each specific profile inside a single .yml file.

In Spring projects, @PropertySource annotation can only be used with .properties

YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way

I hope this article is helpful. Thank you for reading the article. Please like, share and comment. it will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!!!