Spring Boot Interview Questions and Answers

  1. What is Spring Boot ?

Spring Boot is a spring module, a framework, which is used for RAD (Rapid Application development) with extra support of auto configuration and embedded application server.

2. Why is Spring boot used ?

The answer is very simple. It is used for Rapid Application development.

3. How does it provide rapid development of an Application ?

It simplifies the whole development process by Auto configuration thus a developer need not put effort on lot of configuration settings, and it also removes the manual setting of dependencies part. Spring boot auto appends boiler plate codes for configuration and dependencies injection.

4. What is Rapid Application development, RAD ?

RAD is a modified waterfall model. It focuses on development of application in a shorter span of time. There are 5 phases as follows

  • Business Modelling, here business model is designed for the product to be developed.
  • Data Modelling, here relationship between objects is established using information gathered in first phase.
  • Process Modelling, here processes are defined to add, delete, update or retrieve data objects.
  • Application Generation, here actual coding is done.
  • Testing phase and turnover, here product is tested.

5. Is it possible to change the port of embedded Tomcat server in Spring boot?

The answer is yes, go to ‘application.properties’ under resources folder and put server.port = 9001

6. Can we override or replace the embedded tomcat server in spring boot?

Let’s understand, the spring boot starter web is responsible for configuring tomcat server in spring boot application. To exclude it we need to enclose the tomcat under <exclusions> tag in pom.xml file and put jetty instead. Thus customized.

7. Can we disable the default web server in the Spring Boot Application ?

Yes. Goto application.properties and type spring.main.web-application-type=none.

8. How to disable a specific Auto configuration class?

You can use the exclude attribute of @EnableAutoConfiguration annotation, if you find any auto config class not to be applied.

Syntax – @EnableAutoConfiguration(exclude={xyz.class}). Remember this syntax can be used along with the @SpringBootApplication annotation.

9. What is @SpringBootApplication?

@SpringBootApplication allows spring boot application to start the main method and inject all the objects into the Application context of the Spring boot application.

It encapsulates @ComponentScan to enable component scanning, @EnableAutoConfiguration to enable Spring Boot’s auto-configuration feature, and @Configuration to enable Java-based configuration. Thus one annotation for 3. But spring provided loosely coupled features that we can use as per our need.

10. What is @Value Annotation?

This enables us to inject property values into Spring component, values can be anything from spring cloud variables to environment variables. The variables are created in application properties. By default the variable value is picked from @Value annotation. Example below.

employee.age=35 (This is inside application properties)

//Below snippet is to be put inside any class
@Value("${employee.age:No age Property Found}")
String employeeAge;

11. What is a bean class?

Bean class is a normal class which can be used anywhere in Spring boot application, it has 3 rules,

  • No argument constructor,
  • Getter and Setter methods and
  • Implements serializable

12. What is @Component annotation ?

Component annotation is used to recognize the bean as service class. Service class is used to provide output to the controller class. Service class has simple methods which extracts the data from repository. Component annotation registers the bean into the spring application context.

13. What is the purpose of @Autowired ?

Usage of @Autowired, this feature enables inject object dependency implicitly, it internally uses constructors and setter-getter methods, Remember @Autowired annotation cannot be used to inject string or primitive values, it works with reference only.

14. What is @RestController ?

Rest controller accepts client requests and delegates to respective path. It is responsible for the request handling. It is also used to create RestFul web services. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.

It is a specialization of @Controller and is auto detected through the class path scanning. It adds @Controller and @RequestBody annotations. It is typically used with @RequestMapping annotation.

15. How to use a property defined in application.properties file into your java class?

By using @Value annotation, java class can access any variable defined in application.properties. Read question 10 above.

16. Difference between @RestController and @Controller ?

To answer this let’s understand difference between REST API and Web Application. The response in REST API is either JSON or XML, while Web Application’s response is HTML+CSS+JavaScript.

Similarly @RestController returns the object data as JSON or XML. The @Controller maps model object to view or template thus making it appear on web page.

17. What is the difference between @RequestMapping and @GetMapping ?

@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET) .

18. What is the use of profiles in Spring Boot ?

When developing application we typically deal with multiple environments like testing, backup, production etc.

For example – While testing we might use H2 database, while in our production environment we might use SQL.

To make this easy spring provides profiling feature. Separate configuration files are created for each profile. Example application-dev.properties, application-prod.properties along with default application.properties.

Leave a Reply

Your email address will not be published.