19 June, 2019

Top 10 spring boot interview questions for experienced

In this post, we will discuss some top 10 interview questions in spring boot. These questions are tricky and trending now-a-days job market. These interview questions might suitable for 0 to 8 years of experience.

1)  What is @SpringBootApplication does internally ?

Ans :- As per spring boot doc,  @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes. Spring boot enable the developer to use single annotation instead of using multiple. But, as we know spring provided loosely coupled features we can use each individual annotation as per our project needs.


2)  How to exclude any package without using the basePackages filter?

Ans :-  There are many way you can filter any package. But, spring boot provides very tricky option to achieve this without touching the component scan. You can use "exclude" attribute, while using 

the annotation @SpringBootApplication. See the below code snippet.
@SpringBootApplication(exclude= {Employee.class})
public class FooAppConfiguration {}


3)  How to disable a specific auto-configuration class?

Ans :-  You can use "exclude" attribute of @EnableAutoConfiguration. If you find that specific auto-configuration classes that you do not want 
are being applied. 

//By using "exclude"
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

On the other way , if the class is not on the class path, you can use the "excludeName" attribute of the annotation and specify the fully qualified name instead.

//By using "excludeName"
@EnableAutoConfiguration(excludeName={Foo.class})

Also spring boot provides the facility to control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude property. You can add into the application.properties. You can add multiple classes with comma separated.

//By using property filespring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
4)  What is Spring Actuator? What are its advantages?

Ans :-  This is one of the most common interview question in spring boot. As per spring doc definition, "an actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change". 

As we know spring boot provides lots of auto-configuration features which helps the developer to develop ready for production components quickly.But, if you think what about the debugging , how to debug if something goes wrong. As a developer we always need analyze the logs and dig the data flow of our application to check whats going on. So, spring actuator provides a easy access to all those kind of features. It provides many features i.e. what are the beans created, what are the mapping in controller, what is the CPU usage, etc.Automatically auditing, health, and metrics gathering can be applied to your application.

It provides very easy way to access with few production ready REST endpoints to fetch all these kind of information from web. By, using these endpoints you do many things see here the endpoint docs. Nothing to worry about security, if Spring Security is present then these endpoints are secured by default using Spring Security’s content-negotiation strategy. Else , we can configure custom security by the help of RequestMatcher.

5)  How to enable/disable the Actuator ? 

Ans :-  Enabling/Disabling the actuator is easy, the simplest way to enable the features is to add  the dependency to the spring-boot-starter-actuator i.e. Starter. If you don't want the actuator to be enable, then don't add the dependency.

Maven dependency - 
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

Gradle dependency-

dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
}

6)  What is Spring Initializer?

Ans :-  This may not be a difficult question , but the interviewer always checks the subject knowledge of the candidate. Its quite often that you can't expect questions that you have prepared :). However, this is very common question asked frequently in near time.

Spring initializer is a web application , which generates spring boot project with just what you need to start quickly.As always we need a good skeleton of the project, it help you to create a project structure/skeleton properly. Initializer here.

7)  What is shutdown in actuator? 

Ans :-  Shutdown is an endpoint which allows the application to be gracefully shutdown. This feature is not enabled by default.You can enable this by using management.endpoint.shutdown.enabled=true in your application.properties file. But, be careful about this if you are using this.

8)  Is this possible to change the port of Embedded Tomcat server in Spring boot?

Ans :-  Yes, its possible to change the port. You can use the application.properties file to change the port. you need to mention "server.port" (i.e. server.port=8081). Make sure you have application.properties in your project class path, rest spring framework will take care. If you mention server.port=0 , then it will automatically assign any available port.

9)  Can we override or replace the Embedded Tomcat server in spring boot ?

Ans :-  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 dependency as per your project need.

10)  Can we disable the default web server in the spring boot application?

Ans :-  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


Reference documents :-
       Spring endpoints,spring boot annotation, spring servers config

Follow for more details on @Facebook!!!


Find More Questions & Answers Below.

2 comments:

  1. Hello javadevelopersguide,

    Nice blog! I am editor at Java Code Geeks (www.javacodegeeks.com). We have the JCG program (see www.javacodegeeks.com/join-us/jcg/), that I think you’d be perfect for.

    If you’re interested, send me an email to eleftheria.drosopoulou@javacodegeeks.com and we can discuss further.

    Best regards,
    Eleftheria Drosopoulou

    ReplyDelete