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.

17 June, 2019

No main manifest attribute, in sample-hello-microservice-springboot.jar

The error "no main manifest attribute, in sample-hello-microservice-springboot.jar" occurred while trying to execute the jar file. This is very unusual issue with jar file execution. In this case I was trying to run the docker image. The reason of this issue, during the jar execution it was not able to locate the main class. Below is the main class "HelloApplication" and pom file. It seems the spring-boot plugin was missing with config for the manifest (mainClass). 

HelloApplication.java


package com.javadevelopersguide.lab.springboot.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.javadevelopersguide.springboot.business.DeliverHello;
@SpringBootApplication
@EnableAutoConfiguration
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(DeliverHello.class);
}
}

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample-hello-microservice-springboot</groupId>
<artifactId>sample-hello-microservice-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Sample Hello microservice with Springboot.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<!-- Setup Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Solution :-

 I have added the manifest in the pom.xml file. 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample-hello-microservice-springboot</groupId>
<artifactId>sample-hello-microservice-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Sample Hello microservice with Springboot.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<!-- Setup Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.javadevelopersguide.lab.springboot.application.HelloApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now its working fine. You can see the below screenshot to see the result.




Checking on browser.