Top 80+ Spring Interview Questions and Answers for 2023

Top 80+ Spring Interview Questions and Answers for 2023

25 mins read8.4K Views Comment
Updated on May 30, 2023 18:35 IST

Spring interview questions often revolve around various aspects of the Spring framework, including dependency injection, inversion of control, AOP (Aspect-Oriented Programming), transaction management, and Spring MVC. In this blog we have covered questions around these topics that can help candidates demonstrate their knowledge and expertise in Spring development. Let’s understand!

2020_07_Spring-Interview-Questions-and-Answers.jpg

Are you preparing for an interview where the Spring framework is the main part of the tech stack? Here, we have curated a list of popular Spring interview questions you must go through before appearing for your job interview. This blog will provide the latest spring interview questions in one place and ensure you ace your Spring framework interview. For your convenience, we have categorized the Spring interview questions and answers blog under the following topics:

Table of Contents: 

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
350 hours
Free
6 months
– / –
4 months
– / –
– / –
– / –
1 month
50 K
7 weeks
7.8 K
3 months
8.47 K
2 months
7 K
6 months
4 K
2 months

General Spring Interview Questions

Q1. Name different components of a Spring application.

Ans. A Spring application has the following components:

  • Interface – Defines functions
  • Bean class – Consists of properties, its setter and getter methods, functions, etc.
  • Spring Aspect-Oriented Programming (AOP) – Provides the functionality of crosscutting concerns
  • Bean Configuration File – Consists of classes information and the ways to configure them
  • User program – Uses functions

Q2. What is a Spring configuration file?

Ans. The Spring configuration file is an XML file. It has information about the classes, interfaces, and their dependencies.  It suggests how classes are configured and introduced to each other. Spring Configuration file is very useful in managing projects if planned correctly.

Q3. Name various ways to use the Spring Framework.

Ans. Spring Framework can be used as –

  • Full-fledged Spring web application
  • Third-party web framework, using Spring Frameworks middle-tier.
  • Enterprise Java Bean that can wrap existing POJOs (Plain Old Java Objects)
  • Remote usage

Also explore: 

Popular JavaScript Courses Popular Python Courses
Top Java Courses Top Other Programming Languages Courses

Q4. What are the benefits of Spring?

Ans. The various benefits of Spring include –

  • It is versatile and non-invasive
  • Spring supports dependency injection and offers multiple modules to achieve different functionality for the development of the application.
  • It allows developers to develop enterprise-class applications using POJOs.
  • Spring can remove the creation of factory classes.

Q5. How can a Spring file be converted to Java?

Ans. A Spring file is converted to Java by using Spring’sClassPathXmlapplicationContext from Java to set the Spring configuration.

Q6. What are the different features of the spring framework?

Ans. The following are the different features:

  • Transaction Management
  • Inversion of Control
  • MVC framework
  • Aspect-Oriented Programming
  • Lightweight
  • Container

Q7. What are the different types of transaction management?

Ans. There are two types of transaction management supported by Spring that include –

  • Programmatic transaction management
  • Declarative transaction management

Q8. What are some of the vital Spring modules?

Ans. Some of the vital Spring modules are:

  • Spring AOP
  • JDBC
  • MVC
  • Web Module
  • Context
  • DAO

Q9. How can you configure Spring into the application?

Ans. There are three ways to configure Spring into the application, which are –

  • Java-based configuration
  • XML based configuration
  • Annotation-based configuration

Q10. How is Spring Java different from Node JS?

Ans. Following are some key differences between Spring Java and Node JS:

Spring Java Node.JS
Blocking I/O and users must wait for every I/O request Applications are non-blocking I/O (Input/Output model) and asynchronous
Multi-threaded hence tasks are performed simultaneously Single-threaded hence works on the concept of one request, one thread
Expensive as it requires multiple threads Not as expensive

Q11. Is it possible to have multiple Spring configuration files?

Ans. It is possible to define multiple spring context configuration files through ContextConfigLocation. It can consist of multiple locations and be separated by commas and spaces. 

Further, it is also possible to define multiple root-level spring configurations or import elements in the context configuration files.  

Want to learn or test your PHP skills for an interview? Here are some of the most common and frequently asked PHP Interview Questions and Answers with detailed answers. 

Q12. Which channels does Spring 2.0 support?

Ans. Spring 2.0 supports the following channels –

  • Direct Channel
  • Executor Channel
  • Pollable Channel
  • Priority Channel
  • PublishSubscribe Channel
  • Queue Channel
  • Rendezvous Channel
  • Scoped Channel
  • Subscribable Channel

Q13. What are the new features in Spring 4.0?

Ans. Different features of Spring 4.0 include –

  • It manages the Spring Bean lifecycle like JNDI lookup.
  • It is used to create web applications and web services.
  • All annotations are used to create custom-composed annotations.
  • It supports WebSocket-based communications in all web applications.

Q14. What do you mean by the introduction?

Ans. The introduction enables users to add new attributes and methods to existing classes.

Q15. Name different editors used in Spring Framework.

Ans. Different editors used in Spring Framework include –

  • ClassEditor
  • CustomDateEditor
  • FileEditor
  • LocaleEditor
  • PropertyEditor
  • StringArrayPropertyEditor
  • StringTrimmerEditor
  • URLEditor

Q16. How can you control the database connection?

Ans. Database connection can be controlled by using the following classes –

  • AbstractData Source
  • Data Source Utils
  • DataSource TransactionManager
  • DriverManager DataSource
  • SingleConnection DataSource
  • SmartData Source
  • TransactionAware DataSourceProxy

Q17. What is a Property Editor?

Ans. A property editor is a feature of the JavaBeans API. This feature enables the conversion of property values to and from text values.

Q18. What is the Web Module?

Ans. The web module provides basic web-oriented integration features and helps to create a web application without XML. After using the web module, a web.xml file needs to be configured.

Q19. What is Reactive Programming?

Ans. Reactive programming is a non-blocking, asynchronous, and event-driven application. It requires a small number of threads to scale. It has a backpressure mechanism. The back pressure is the key ingredient ensuring the producers do not overwhelm consumers.

Q20. What are the advantages of Reactive Programming?

Ans. The following are some of the advantages of Reactive Programming: 

  • It helps to increase the utilization of computing resources, such as multicore and multi-CPU hardware.
  • Complex threading becomes very easy. 
  • It helps to increase the performance by a reduction in the serialization process.
  • Easy to implement back-pressure

Q21. How to access Hibernate using the Spring framework? 

Ans. There are two methods to access Hibernate using the Spring framework:

  • Extend the HibernateDAOSupport. After this, apply the AOP Interceptor node
  • Inversion of Control with a Hibernate Template and Callback

Q22. Define Pointcut in Spring. 

Ans. Pointcut in Spring is an expression language of AOP. It matches the target methods to apply the advice.

Spring Beans Interview Questions

 Q23. What is a Spring Bean?

Ans. Spring Bean is a crucial concept of the Spring Framework. Objects that form the base of any application and those managed by the Spring IoC container are called beans.

Q24. Why is the Spring Bean configuration file necessary?

Ans. Spring Bean configuration file can be used to define all the beans initialized by Spring Context. Spring Bean configuration file reads and initializes the Spring Bean XML file when the instance of Spring ApplicationContext is created. These files can then get different bean instances once the context is initialized.

Q25. What are Inner Beans?

Ans. If a bean is using the property of another bean, it can be called an inner bean. Spring’s XML-based configuration metadata defines the use of <bean> element inside the <property> or <constructor-arg>. Inner beans are anonymous.

Also Read: Top JavaScript Interview Questions and Answers

Q26. What is Bean Wiring?

Ans. The structure is known as bean wiring if beans get combined within the Spring container. These beans are usually combined by using the dependency injection method.

Q27. What is Bean Factory?

Ans. Bean Factory is a Lightweight container that loads bean definitions and manages beAns.  These beans are configured using an XML file and manage singleton-defined beans.

Q28. What are the different ways of controlling the life cycle events of a bean?

Ans. There are 2 ways to control the life cycle events of a bean –

  • InitializingBean and DisposableBean interfaces
  • Init-method and destroy-method

Q29. Are Spring singleton beans thread-safe?

Ans. No, Spring singleton beans are not thread-safe.

Q30.  What is Autowiring?

Ans. Autowiring is a crucial feature in the Spring framework, which uses setter or constructor injection and allows the user to inject the object dependency implicitly.

Q31. How many types of Spring Bean auto wiring are there?

Ans. Spring framework has four types of auto wiring –

  • autowire byName
  • autowire byType
  • autowire by constructor
  • autowiring by @Autowired and @Qualifier annotations

Q32. What are the limitations of auto wiring?

Ans. The following are the limitations of auto wiring:

  • Overriding possibility: Dependencies can be specified using <constructor-arg> and <property> settings. It will always override autowiring.
  • Primitive data type: The primitive data types String or Integer using property or constructor-args tag need to be defined. These tags cannot be autowired.
  • Confusing Nature: If there is a lot of dependency in a program, then it will be hard to find using autowire attribute of the bean. Always prefer using explicit wiring because autowiring is less precise.

Q33. Mention the five bean scopes supported by Spring?

Ans. Following are the five bean scopes:

  • Request
  • Global-Session
  • Session
  • Singleton
  • Prototype

Q34. What are Singleton Scopes used with?

Ans. Singleton Scopes are used with Stateless session bean.

Q35. Explain Bean Lifecycle.

Ans. The bean lifecycle defines the path of bean initiation and lasts till bean completion. Spring containers manage it as they start as soon as the program is initiated. Further, as per the request, it creates an instance of a bean and then injects the dependencies. Once this process is done, the bean is destroyed as soon as the spring container is closed. 

Below is the bean lifecycle flow:

Container started → bean instantiated → dependencies injected → custom int() method → custom utility method → custom destroy() method

Also Read: Top Python Interview Questions and Answers

Spring AOP Interview Questions

Q36. Name some AOP concepts.

Ans. Different types of AOP concepts are –

  • Interceptor
  • Joinpoint
  • Aspect
  • Target object
  • Weaving
  • Pointcut
  • Advice
  • AOP proxy

Q37. Name the types of advice in AOP.

Ans. There are five types of advice in AOP –

  • Throws advice
  • Around advice
  • After returning advice
  • Before advice
  • After advice

Q38. What is the AOP module?

Ans. Aspect-Oriented Programming (AOP) is a key component of the Spring framework. It allows interceptors to intercept an application. It can also be defined as the breaking of codes into different modules.

Q39. What is weaving?

Ans. Weaving is applying aspects to the target object or other application types to create an advised object or a new proxy object.

Q40. On what points can you apply weaving?

Ans. We can apply weaving on the following points –

  • Compile Time
  • Class load Time
  • Runtime

Q41. Name some of the types of Autoproxying.

Ans. Different types of AutoProxying are:

  • Metadata autoproxying
  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator

Q42. Explain JoinPoint.

Ans. JoinPoint showcases a point in an application where we can plug in an abstract-oriented programming aspect. This is where actions will be taken with the help of the Spring AOP framework.

Looking to expand your IT skills and knowledge? Here is a list of Top Online IT Courses that will improve your IT skills and career prospects.

Dependency Injection/ IoC Interview Questions

Q43. What is Dependency Injection?

Ans. Dependency Injection is one of the important functionalities of Spring IoC (Inversion of Control) and is responsible for injecting dependencies through different processes, either the Constructor or Setter method. Dependency Injection also ensures loose coupling between the classes.

Q44. What are the different types of Dependency Injection?

Ans. Dependency Injection is of three types –

  • Constructor injection – Here a class constructor provides dependencies
  • Setter injection – Here, the client exposes a setter method the injector uses to inject the dependency.
  • Interface injection – Here the injector method provides dependency

Only constructor and setter injections are used in the Spring framework.

Q.45. What is Inversion of Control (IoC)?

Ans. Inversion of Control (IoC) is a design principle central to the Spring framework. It refers to the concept of transferring control over the flow of an application from the application itself to an external container or framework.

This inversion of control offers several benefits. It promotes loose coupling between components, as the application doesn’t need to be aware of the concrete implementations of its dependencies. It also enables easier testing, as dependencies can be easily mocked or replaced. Additionally, it enhances modularity and reusability, as components can be easily swapped or extended without modifying the application code.

Q46. What are the benefits of using IoC?

Ans. The primary benefits of IoC include –

  • Minimizes the number of codes in any application
  • It makes a platform/application easy to test, as it doesn’t need singletons or JNDI lookup mechanisms
  • Promotes loose coupling with fewer efforts and intrusive mechanism
  • Facilitates Eager Instantiation

Q47. Can you inject an empty string value?

Ans. Yes, we can inject an empty string value as the default value.

Q48. What do you mean by loose coupling?

Ans. Coupling is associated with the collaboration of two types – tight and loose coupling. 

Loose Coupling refers to removing one object’s dependency on another object (ClassX’s dependence on ClassY). It generally helps in:

  • Improving test-ability
  • Following GOF principle
  • Swapping pieces of codes and objects

Tight Coupling – This form of coupling refers to the concept where when one object changes, the other is also compelled to change. 

Q49. What is the difference between constructor injection and setter injection?

Ans. The differences between constructor injection and setter injection are: 

Constructor Injection Setter Injection
No partial injection. Partial injection can be present.
It does not override the setter property. It overrides the constructor property.
A new instance is created if any modification is done. The new instance is not created if any modification is done.

Q50. State difference between BeanFactory and ApplicationContext.

Ans. Spring involves two different IoC containers named BeanFactory, and other is ApplicationContext: Below are some key differences between both of the IoC containers:

BeanFactory ApplicationContext
No support for Annotation based dependency injection Supports annotation
Does not supports publishing event to beans Supports publishing events to beans 
Does Not support internationalization Supports internationalization
Slow loading Fast loading

Q51. What is DataAccessException?

Ans. DataAccessException is an unchecked RuntimeException. It is used to handle the errors occurring when the details of the database access API are being used.

Q52. How will you access Hibernate using Spring?

Ans. We can access Hibernate using Spring in two ways –

  • Inversion of Control with a Hibernate Template and Callback
  • Extending HibernateDAOSupport and Applying an AOP Interceptor node

Q53. What is the procedure for providing configuration metadata to the Spring Container?

Ans. The following are three important methods to provide configuration metadata to the Spring Container:

  • Annotation-based configuration
  • XML-based configuration file
  • Java-based configuration

Q54. What is the procedure to inject a Java Collection?

Ans. Following are the different types of configuration elements:

  • The <set> type injects a set of values without any duplicate values.
  • The <list> type is used to inject a list of values. In this case, duplicates are allowed.
  • The <props> type can inject a collection of name-value pairs. In this case, both name and value should be Strings.
  • The <map> type injects a collection of name-value pairs. In this case, name and value can be of any type.

Explore popular Java Courses, Skills & Careers, and read our blog – what is Java?

Spring Annotations Interview Questions

Q55. What are some of the important Spring annotations?

Ans. A list of some of the Spring annotations are:

  • @Service is used to indicate a class as a Service. 
  • @Scope  is used to configure the scope of the spring bean.
  • @Configuration is used to indicate that the class can be used as a source of bean definitions by the Spring IoC container.
  • @ComponentScan is used to scan all the classes available under a package.
  • @Required indicates the affected bean property that must be populated at configuration time by an explicit property value in a bean definition or with the help of autowiring. 
  • @ResponseBody is used to send an object as a response, basically used for sending XML or JSON data as a response.
  • @PathVariable maps dynamic values from the URI to handler method arguments.
  • @Autowired is used for autowire bean on the setter method similar to @Required annotation, on the constructor, on a property, or methods with arbitrary names and/or multiple arguments.
  • @Component indicates a class as a component, where all the classes are used for auto-detection and composed as a bean when annotation-based configurations are used.
  • @Controller is used for MVC applications and is generally used with @RequestMapping annotation.
  • @Repository annotation indicates that a component is used as a mechanism to store/retrieve/search data.
  • @Qualifier is used for wiring a single bean with a property at a time when there are multiple beans of similar type with the help of @Autowired annotation
  • @Bean is used in java based configurations, where a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

Q56. Can you name different types of events for Listeners?

Ans. Yes, there are three types of events for listeners:

  • ContextClosedEvent – When the context is closed.
  • ContextRefreshedEvent – When the context is initialized or refreshed
  • RequestHandledEvent – When the web context handles a request

Q57. What is the annotation-based (@AspectJ based) aspect of implementation?

Ans. The given implementation case (@AspectJ based implementation) leads to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

Q58. How to go ahead with exceptional handling in Spring?

Ans. There are multiple ways to handle exceptions in Spring, and some of them are:

  1. Controller-based annotation method using @ExceptionHandler annotation
  2. Global Exception Handler, along with  @ControllerAdvice annotation
  3. HandlerExceptionResolver implementation for creating a global exception handler

Q59.  How to send an email by using Spring Framework?

Ans. The JavaMailSenderImpl annotation of Spring helps in easily sending emails from Java. This JavaMailSenderImpl produces a JavaMailSender interface and supports the following:

  • JavaMail API’s MimeMessages 
  • Spring’s SimpleMailMessage

Further, it allows the class to pre-define the mail settings and pre-configure JavaMail Session. The below steps will help in sending an email using the Spring framework:

  1. In ApplicationContext.xml – Configure JavaMailSenderImpl as Spring bean
  2. With JavaMailSenderImpl reference, configure the test application
  3. Create a Java application to send an email

Note: If you are not very comfortable with writing Spring configuration files, you can sign-up for Spring online course like Spring Framework 5: Beginner to Guru on Udemy as it thoroughly covers Spring Framework concepts)

Q60. How are The Spring @Controller and @RestController Annotations different from each other?

Ans. Below are the differences:

@Controller @RestController
A very traditional Spring controller Introduced in Spring 4.0 to offer effective RESTful web services.
Includes only the components of @Controller Combines @Controller and @ResponseBody
Designates the bean class Eliminates annotation needs for handling every request of the controller class

Q61. Is it possible to send an object as the response of the Controller handler method?

Ans. Yes, it is possible to send JSON or XML-based responses in restful web services with the help of @ResponseBody annotation.

Preparing for a Java interview? Here are the Top Java Interview Questions and Answers to boost your interview preparation. 

Data Access Interview Questions

Q62. What is Spring DAO?

Ans. In Spring, DAO stands for Data Access Object, a design pattern that provides an abstract interface to some database. The basic objective of DAO is to ease the data access technologies, including JDBC and Hibernate. It offers data operations without exposing the exact database details. Further, a DAO pattern is used to perform CRUD operations. 

Explore Popular Database Courses on Shiksha Online

Q63. Name some classes present in spring JDBC API.

Ans. The following are some of the important JDBI API classes:

  • NamedParameterJdbc Template
  • Jdbc Template
  • SimpleJdbc Template
  • SimpleJdbcInsert 
  • SimpleJdbcCall

Q64. What is the possible way to fetch records by Spring JdbcTemplate?

Ans. The following are the two interfaces that can be used to fetch records from the database:

  • RowMapper
  • ResultSetExtractor

Q65. How is Spring JDBC different from plain JDBC?

Ans. Spring JDBC is a value-add provided by the Spring Frameworks on the top JDBC layer. It is an abstraction layer on top of JDBC technology. Spring JDBC does the following: 

  • Do the work for each iteration
  • Prepare and execute the statement
  • Process any exception
  • Define connection parameters
  • Handle transactions
  • Set up the loop to iterate through the results (if any)
  • Open the connection
  • Specify the statement
  • Close the connection

Q66. What ORMs are supported by Spring?

Ans. Spring supports the following ORMs:

  • TopLink
  • Hibernate
  • JPA (Java Persistence API)
  • iBatis
  • OJB
  • JDO (Java Data Objects)

Q67. How would you access hibernate with the help of Spring?

Ans. There are two ways to hibernate using Spring:

  • By extending hibernateDaoSupport and spreading over an AOP interceptor
  • By inversion of control (IOC) with a HibernateTemplate and Callback

Q68. How is the context:annotation-config and context:component-scan different from each other?

Ans. Both context:annotation-config and context:component-scan are the two different XML configuration elements of Spring. 

 <context:annotation-config> is used for:

  • Activating annotations in beans that are registered in the application context
  • Activating dependency injection annotations
  • Resolving dependencies like @Autowired, @Qualifier, @PostConstruct, @PreDestroy, and @Resource 

<context:component-scan> is used for:

  • Identifying the right packages which need to be scanned and finding annotated beans or components
  • Detecting @Component, @Repository, @Service, @Controller, @RestController, and @Configuration

MVC (Model-View-Controller) Interview Questions

Q69. What is Spring MVC?

Ans. Spring MVC follows the Model-View-Controller design pattern and implements basic features of a core Spring framework, including Inversion of Control and Dependency Injection.

Q70. How is the DispatcherServlet used in Spring MVC?

Ans. The DispatcherServlet is a popular Spring MVC web framework that acts as Front Controller. A Front Controller receives all the information about incoming requests and further delegates this request to other components (@Controller annotation & InternalResourceViewResolver class). 

Also, it is noticeable that a DispatcherServlet aptly handles incoming requests and forwards them to the right Controller depending upon its actual role. It also plays a crucial role in activities like:

  • View Resolution
  • Error handling
  • Locale resolution
  • Theme resolution

Q71. Give steps to configure DispatcherServlet in Spring. 

Ans. Steps to configure DispatcherServlet in Spring:

<servlet> 

<servlet-name>dispatcher</servlet-name>

<servlet-class> 

org.springframework.web.servlet.DispatcherServlet 

</servlet-class> 

<init-param> 

<param-name>ConfigLocation</param-name> 

<!–Defaults to ABC.xml –> <param-value>ABC.xml

</param-value> 

</init-param> 

</servlet> 

Want to upskill yourself to build a successful career? Check out the Most In-Demand Tech Skills.

Q72. What is the procedure for uploading files in the Spring MVC Application?

Ans. Spring MVC Application provides built-in support to upload files using MultipartResolver interface implementations. Here, a validator is used to check the file uploads in the field, which should consist of a size greater than zero. In the end, a simple view consists of a form with the option to upload a file.

Q73. How do you validate data in Spring Web MVC Framework?

Ans. There are three  ways to implement validation: 

  • By using annotation
  • Manually
  • By using both functions

Q74. What is a RowCallbackHandler?

Ans. RowCallbackHandler is an interface used by JdbcTemplate to process rows of a ResultSet. It processes rows on a per-row basis.

Q75. What is ViewResolver class?

Ans. The ViewResolver class offers to map between view names and actual views. It also allows the user to set properties like prefixes or suffixes to the view name.

Q76. Which would you consider, Play framework or Spring MVC?

Ans. I would go with Spring MVC for these reasons:

  • Availability of ample resources on Spring MVC.EBOOK
  • It renders a clean division among views, controllers, and JavaBean models.
  • Truly view-agnostic
  • It has a better testable code and ensures better integration with view technologies.

Also Read: Python Vs Java – Which One is Better to Learn?

Spring Boot Interview Questions

Q77. What do you mean by Spring Boot?

Ans. From the Spring umbrella, a Spring Boot is a Java-based framework that helps in creating microservices that helps in building stand-alone spring applications that are production-ready. Some of the advantages of using a Spring Boot framework are:

  • Eases the process of developing spring applications
  • Boosts productivity
  • Reduces development time of application 
  • Offers flexibility to configure with XML and Java Beans

Q78. Differentiate between SpringBoot and Spring MVC.

Ans. The differences between SpringBoot and Spring MVC are: 

SpringBoot Spring MVC
It is a way to ease the creation of a stand-alone application with basic configurations Spring MVC is a module of Web-Servlet which is used for implementing a web application
It offers a unique feature like auto-configuration of application It is equivalent to JSF(Java Server Faces) in the JavaEE stack
SpringBoot offers embedded HTTP servers such as Jetty, Tomcat for creating executable files It has an equivalent @RestController for implementation of REST 

Q79. What are the features of Spring Boot?

Ans. The following are the features of Spring Boot: 

  1. Spring Initializer 

It is a tool that bootstraps the spring boot projects. Spring Initializer enables a developer to create an internal project structure. It enables developers to select a project, the programming language of choice, and add dependencies such as dev tools, actuator, and web. While using this feature, the developer will not have to manually set up the structure of the project.

  1. Spring Boot CLI 

It is a command-line tool that allows you to develop a Spring application quickly. Spring CLI lets you execute Groovy scripts, so you can code by using your knowledge of Java, without writing reusable code repeatedly. 

  1. Starter Dependency 

This feature enables Spring Boot to aggregate common dependencies together and improve productivity.

  1. Auto-Configuration 

Auto-Configuration helps you to configure your Spring Boot application automatically. While creating a new project, Spring Boot allows you to choose dependencies for your project. Based on these dependencies, the autoconfiguration feature loads specific default configurations.

  1. Externalized Configuration

It means using the application code that you use in one environment in an external environment. Externalized configuration feature lets you reuse your code in diverse environments.

Q80. What are the advantages of using Spring Boot for application development?

Ans. The advantages of using Spring Boot for application development are: 

  1. Apps developed using spring boot are easier to launch. It helps to create applications that can be started using java.jar.
  2. Spring boot offers pinpointed ‘started’ POMs to Maven configuration.
  3. It provides a variety of plugins to develop and test Spring Boot applications easily using Build Tools like Maven and Gradle.
  4. It has the provision to embed Undertow, Tomcat, Jetty, or other web servers directly. You don’t need to deploy WAR files.
  5. Offers production-ready support like monitoring.
  6. It avoids writing lots of boilerplate Code, Annotations, and XML Configuration.
  7. Easy to integrate Spring boot application with its Spring ecosystems like Spring JDBC and Spring Security.

Also Read: Kotlin vs Java: Why Should You Choose Kotlin Over Java

Q81. What are the different ways to create a Spring Boot application using Maven?

Ans. The following are some of the ways to create a Spring Boot application using Maven:

  • Spring Boot CLI
  • Spring Initializr
  • Spring Starter Project Wizard
  • Spring Maven Project

Q82. Explain Spring Boot starters.

Ans. Spring Boot starters are a set of dependency descriptors that you can include in your application to enable dependencies. The starters contain many dependencies that you need to get a project up and running quickly. Spring Boot provides several starters that allow us to add jars to the classpath. Some of the Spring Boot starters under the org.springframework.boot group are: 

  • spring-boot-starter  – It is the core starter that includes logging, auto-configuration support, and YAML.
  • spring-boot-starter-artemis – for JMS messaging with Apache Artemis
  • spring-boot-starter-data-jpa – for Spring Data JPA with Hibernate
  • spring-boot-starter-data-couchbase – for the Couchbase document-oriented database
  • spring-boot-starter-web-services – for Spring Web Services
  • spring-boot-starter-thymeleaf – to build MVC web applications using Thymeleaf views
  • spring-boot-starter-data-ldap – for Spring Data LDAP
  • spring-boot-starter-security – for Spring Security

Q83.What is the use of @SpringBootApplication annotation?

Ans. The @SpringBootApplication annotation is a combination of three Spring annotations, namely  @Configuration, @ComponentScan, and @EnableAutoConfiguration. It offers the features of all three annotations with just one line of code. It enables developers to use a single annotation instead of multiple annotations, which results in fewer lines of code.

The @SpringBootApplication annotation offers a host of functionalities through the following three Spring annotations:

@Configuration Annotation

It marks a class as a Configuration class for Java-based configuration. 

@ComponentScan Annotation

It allows component scanning so that the web controller classes and other components that are created will be automatically discovered and registered as beans in Spring’s Application Context. This annotation discovers all the @Controller classes you write.

@EnableAutoConfiguration Annotation

It enables Spring Boot to auto-configure the application context.

2020_12_Spring-Boot-Application.jpg

Q84. What is Thymeleaf?

Ans. Thymeleaf is a server-side Java template engine used for creating web applications. It offers various template modes including HTML, XML, CSS, and text. Thymeleaf can be integrated with Spring Framework. Spring Boot offers auto-configuration for Thymeleaf by adding the spring-boot-starter-thymeleaf dependency. 

Thymeleaf Implementation: 

<dependency>  

<groupId>org.springframework.boot</groupId>  

<artifactId>spring-boot-starter-thymeleaf</artifactId>  

</dependency>

Preparing for an HTML interview? These Top HTML Interview Questions and Answers will help you crack your HTML interviews. 

Q85. How to deploy Spring Boot Applications to an external Tomcat server?

Ans. To deploy your Spring Boot application to an external tomcat server, you will need to do the below changes:

  • pom.xml – add dependency and packaging to war
  • Extend the main class with SpringBootServletInitializer and override its configure method
  • Generate WAR and deploy to the external server

Q86. How to use jetty instead of tomcat in spring-boot-starter-web?

Ans. We can use jetty instead of tomcat in spring-boot-starter-web by replacing the existing dependency. 

Step 1: Find the following dependency: 

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-web</artifactId>

</dependency>

Step 2: Replace it with: 

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

Step 3: Add Jetty: 

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jetty</artifactId>

</dependency>

All the Best for your interview, and watch out for this space for more Spring interview questions and answers!

FAQs

What are the important topics in Spring that you must learn?

If you plan to learn Spring, you must learn about Aspect-Oriented Programming (AOP), Spring Bean Lifecycle, Container, Dependency and IOC and Spring Boot Actuator.

What is @bean spring?

Beans are the objects that are the backbone of the application. These are initiated, assembled as well as managed by Spring IoC container.

Does spring support tight coupling?

Yes, it does support tight coupling.

Why is Spring framework lightweight?

The Spring framework is lightweight because of its size and functionality. It is lightweight because you do not need to use the entire spring to use a part of it. For example, you can will be able to use Spring JDBC without Spring MVC.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio