Learning Path for Programming Journey. Start Now

Spring Interview Questions

Spring Interview Questions

Hackr.io.

Spread the Knowledge

Ever since its release back in October of 2002, Spring Framework has succeeded in becoming a top application framework for Java developers. In fact, it secures a place among the leading backend web development frameworks. Hence, opting for a career in Spring Framework is very lucrative.

Spring Interview Questions

Spring Framework has a wide array of concepts that one needs to have a working understanding in order to stay ahead in the game. If you’re preparing for a Spring job interview, then here are 20 essential Spring interview questions that you must know the answers to:

Q: Please list down the various features of Spring Framework and advantages of using the same.
A:

Features:

  • Allows creating and managing the configuration and lifecycle of application objects
  • AOP (Aspect Oriented Programming) provides support for unified development by separating application business logic from system services
  • Highly configurable MVC web application framework with the ability to easily switch to other frameworks
  • Instead of looking for or creating dependent objects, the objects give their dependencies. This design principle is known as IoC (Inversion of Control)
  • Lightweight
  • Offers a generic abstraction layer for transaction management that can also be used in container-less environments
  • The JDBC abstraction layer offers an exception hierarchy that simplifies error handling

Advantages:

  • Enables POJO (Plain Old Java Object) Programming that further enables continuous integration and testability
  • Open-source with no vendor lock-in
  • Simplified JDBC because of DI (Dependency Injection) and Inversion of Control
  • Thanks to the layered architecture, it’s easy to keep what you want and discard what you don’t

Q: How many modules does the Spring Framework have?
A: Spring Framework has about 20 modules. Each of them is classified into one of the following layers:

  • Spring Core Container – The basic core layer of the Spring Framework. It has the following modules:
  • SpEL (Spring Expression Language)
  • Spring Bean
  • Spring Core
  • Spring Context
  • Data Access/Integration – This is the layer responsible for providing support for interacting with the database. It contains the following modules:
  • JDBC (Java DataBase Connectivity)
  • JMS (Java Messaging Service)
  • ORM (Object Relational Mapping)
  • OXM (Object XML Mappers)
  • Transaction
  • Web – Providing support for creating web applications is the responsibility of this layer. It has these modules:
  • Web
  • Web – MVC
  • Web – Portlet
  • Web – Socket
  • Aspect Oriented Programming – Allows using Advices, Pointcuts, and others to decouple the code
  • Instrumentation – Provides support for class instrumentation and classloader implementations
  • Test – Responsible for providing support for accomplishing testing using JUnit and TestNG

Some additional Spring Framework modules are:

  • Aspects – Offers support for integrating with AspectJ
  • Messaging – Provides support for STOMP and an annotation programming model used for processing as well as routing STOMP messages from WebSocket clients

Q: What components does a Spring application have?
A: A typical Spring application can be subdivided into the following components:

  • Bean Class – Contains properties, functions, setter and getter methods, et cetera
  • Bean Configuration File – Contains information on classes as well as how to configure the same
  • Interface – Defines the functions
  • Spring Aspect Oriented Programming – Provides functionality of cross-cutting concerns
  • User Program – Uses the function

Q: What do you understand by the Spring IoC Container? Explain their types.
A: The Spring IoC container lies at the core of the Spring Framework. The container uses Dependency Injection for managing various Spring application components.

The IoC container is responsible for creating the objects, configuring them, wiring them together, and managing their lifecycle. The container receives instructions about the same from the provided configuration metadata.

Means for providing the configuration metadata can include Java annotations, Java code, or XML. There are two types of IoC containers in Spring:

  • ApplicationContext – Provides additional functionality. It is built on top of the BeanFactory interface.
  • BeanFactory – A prepackaged class containing a collection of beans. Instantiates the bean whenever as required by the clients

Q: Please explain Dependency Injection in Spring. In how many ways can the same be used?

A: Instead of creating objects directly, Dependency Injection allows defining how objects should be created. As such, the code doesn’t directly contain connecting components and services together.

The configuration file has the information on which services are needed by which components. The IoC container is responsible for connecting components with the appropriate services. Dependency Injection can be used in the following forms:

  1. Construction Injection
  2. Setter Injection

Q: Can you differentiate between ApplicationContext and BeanFactory in Spring?
A:

  • Annotation Based Dependency – BeanFactory doesn’t support annotation-based dependency while ApplicationContext does
  • Interface Definition – BeanFactory interface is defined in org.springframework.beans.factory.BeanFactory while the ApplicationContext interface is defined in org.springframework.context.ApplicationContext
  • Internationalization Support – While ApplicationContext supports internationalization, BeanFactory doesn’t
  • Object Management – BeanFactory uses syntax for providing a resource object. Contrarily, ApplicationContext creates as well as manages resource objects on its own
  • Type of Initialization – ApplicationContext makes use of eager or aggressive initialization. On the other hand, BeanFactory uses lazy initialization

Q: How is the configuration metadata provided to the Spring container?
A: There are three ways in which the configuration metadata is provided to the Spring container, enumerated as follows:

  • Annotation-based Configuration – By default, annotation wiring is turned off in the Spring container. Using annotations on the applicable class, field, or method declaration allows it to be used as a replacement of using XML for describing a bean wiring.
  • Java-based Configuration – This is the newest form of configuration metadata in Spring Framework. It has two important components:
    1. @Bean annotation – Same as that of the <bean/> element
    2. @Configuration annotation – Allows defining inter-bean dependencies by simply calling other @Bean methods in the same @Configuration class
  • XML-based Configuration – The dependencies, as well as the services required by beans, are specified in configuration files that follow the XML format. Typically, these configuration files contain several application-specific configuration options and bean definitions.

Q: What do you understand by Spring Beans? How many bean scopes are supported by Spring Framework?
A: Configured, instantiated, managed, and wired by the Spring IoC container, Spring Beans are the objects that form the core of a Spring application. Spring Beans are created with the configuration metadata supplied to the Spring IoC container.

Spring Framework provides support for a total of 5 scopes:

  • Global-session* – Provides scope for a bean definition to a Global HTTP-session
  • Prototype – Provides scope for a single bean definition for having any number of object instances
  • Request* – Provides scope for a bean definition to an HTTP-request
  • Session* – Provides scope for the bean definition to a single instance per Spring IoC container
  • Singleton – Provides scope for the bean definition to a single instance per Spring IoC container

* 🡪 Available only when using a web-aware ApplicationContext.

Q: Please explain the Bean lifecycle in Spring Bean Factory Container?
A:

  1. The bean lifecycle starts with the Spring IoC container instantiating the bean from the bean’s definition present in the XML file
  2. As specified in the bean definition, Spring then populates all properties using DI
  3. If the bean implements the BeanNameAware interface then the setBeanName() method is called by passing the bean ID
  4. If the bean implements the BeanFactoryAware interface, the setBeanFactory() method is called by passing an instance of the bean
  5. If there are any BeanPostProcessors associated with the bean then the preProcessBeforeInitialization() methods are called
  6. The init method is called if it is specified for the bean
  7. At last, the postProcessAfterInitialization() methods are called if there are any BeanPostProcessors associated with the bean

Q: What is Annotation-based container configuration? Also, explain how to turn on annotation wiring in Spring Framework.
A: Annotation-based container configuration is an alternative to XML setups. Rather than using XML for describing a bean wiring, the developer moves the configuration to the component class by using annotations on the appropriate class, field, or method declaration.

Because annotation wiring is turned off by default, it needs to be turned on before it can be used. It is done by configuring the <context:annotation-config/> element in the Spring configuration file.

Q: Please explain the various annotations supported by Spring.
A:

  • @Autowired – Used for autowiring bean on the setter methods, a property, constructor or methods with arbitrary names or several arguments. It provides precise control over how and where the autowiring needs to be done.
  • @Component – A generic stereotype for a Spring-managed component, it marks a Java class as a bean that can be picked up by a component-scanning mechanism and pull it into the application context.
  • @Controller – Marks a class as a Spring Web MVC controller. Beans marked with this annotation are automatically imported into the Dependency Injection container.
  • @Qualifier – Used along with @Autowired annotation for specifying that only one of the several yet alike beans, needs to be wired.
  • @Repository – A specialization of the component annotation with almost identical use and functionality. Specifically, it provides additional benefits for DAOs (Data Access Objects).
  • @RequestMapping – Maps a particular HTTP request method to a specific class or method in controller responsible for handling the respective request.
  • @Required – Applied to bean property setter methods, it indicates that the bean property needs to be populated at the configuration time with the use of an explicit property value present in a bean definition or via autowiring. In case the bean property is not populated, the container throws the BeanInitializationException message.
  • @Service – Another specialization of the component annotation. Although it doesn’t offer any additional behavior over the component annotation, it can be used over the @component annotation in service-layer classes for specifying the intent in a better way.

Q: What do you mean by Spring DAO support?
A: The Spring DAO support eases working with data access technologies, such as JDBC, JDO, and Hibernate, in a reliable way. Also, it allows coding without worrying about catching specific-technology exceptions and easily makes a switch amongst persistence technologies.

Q: What classes does the JDBC API contain?
A:

  • JdbcTemplate
  • NamedParameterJdbcTemplate
  • SimpleJdbcCall
  • SimpleJdbcInsert
  • SimpleJdbcTemplate

Q: How will you access Hibernate using Spring Framework?
A: Hibernate can be accessed using Spring Framework in the following two ways:

  1. Extending HibernateDAOSupport and then applying an AOP Interceptor node
  2. Inversion of Control with a Hibernate Template and Callback

Q: Enumerate the types of transaction management supported by Spring.
A: Spring Framework provides support for two types of transaction management:

  • Declarative transaction management – While the transaction management is separated from the business code, only annotations or XML-based configurations are used for managing transactions.
  • Programmatic transaction management – The transaction is managed with programming. Although extremely flexible, it is very difficult to maintain.

Q: Please explain the AOP technique.
A: AOP or Aspect-Oriented Programming is a programming technique that allows programmers to modularize behavior that cuts across the typical division of responsibility or cross-cutting concerns. Logging and transaction management are examples of cross-cutting concerns.

Q: What is Advice in Spring? Explain its various types.
A: Any action taken by an aspect at some particular joinpoint in Spring Framework is called an Advice. Spring AOP makes use of advice for maintaining a chain of interceptors “around” the joinpoint i.e. as an interceptor. Advice can be of the following types:

  1. After (finally) – Configured using the @After annotation mark, it is executed after a joinpoint method, whether exiting normally or throwing an exception
  2. After returning – Configured using the @AfterReturning annotation mark, it is executed right after the joinpoint method completes normal execution
  3. After throwing – Configured using the @AfterThrowing annotation mark, it is executed if and only if the jointpoint method exits by throwing an exception
  4. Around – Configured using the @Around annotation mark, it is executed before as well as after a joinpoint method
  5. Before – Configured using the @Before annotation mark, it is executed before the joinpoint method

Q: Could you draw a comparison between concern and cross-cutting concern in Spring AOP?
A: While the concern is a behavior that the developer wants to have in a particular module of a Spring application, the cross-cutting concern is a concern that is applicable throughout the entire Spring application.

Q: What do you understand by the Spring MVC framework?
A: The Spring MVC framework is responsible for providing model-view-controller architecture as well as ready-to-use components, used for developing flexible and loosely coupled web apps.

The MVC pattern helps in separating out the various aspects of the application, such as business logic, input logic, and UI logic, in addition to providing a loose coupling amongst these separated elements.

Q: Please explain DispatcherServlet.
A: The DispatcherServlet is the essence of Spring Web MVC framework and handles all the HTTP requests as well as responses. Upon receiving the entry of handler mapping from the configuration file, the DispatcherServlet forwards the request to the controller.

Thereafter, the controller returns an object of Model and View. Afterward, the Dispatcher Servlet checks the configuration file for the entry of view resolver and calls the specified view component.

So, that completes the list of the 20 essential Spring interview questions. These questions will surely help you buckle up for your Spring interview. All the very best!

Be sure to check out these best Spring tutorials to further refine your Spring knowledge.

You also might be Interested in Other Interview Questions:

Related Posts

Your email address will not be published. Required fields are marked *

*