21 Spring Interview Questions and Answers


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 21 essentials Spring interview questions that you must know the answers.

Q1). What is Spring?

    Spring is an open-source development framework for Enterprise Java. The core features of the Spring Framework can be used in developing any Java EE platform. Spring framework targets to make Java EE development easier to use and promotes good programming practice by enabling a POJO- based programming model.

Q2). Please list down the various features of the Spring Framework and the advantages of using the same.  

  1. Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of the spring framework is around 2MB.
  2. Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
  3. Aspect-oriented (AOP): Spring supports Aspect-oriented programming and separates application business logic from system services.
  4. Container: Spring contains and manages the life cycle and configuration of application objects.
  5. MVC Framework: Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.
  6. Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
  7. Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.
Q3) What components does a Spring application have?
A typical Spring application can be subdivided into the following components:
  • Bean Class - Contains properties, functions, setter and getters methods, etc.
  • Bean Configuration file- Contains information on classes as well as how to configure the same.
  • Interface - Defines the functions.
  • Spring Aspect Oriented Programming - Provides the functionality of cross-cutting concerns.
  • User Program- Uses the function.
Q4) What do you understand by the Spring Ioc Container? Explains their types.
1. The Spring Ioc container lies at the core of the Spring Framework. The container uses Dependency Injection for managing various Spring application components.
2. The Ioc container is responsible for creating the objects, configuring them, writing them together, and managing their lifecycle. The container receives instructions about the same from the provided configuration metadata.
3. 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.

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

1. 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.2. 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:

i. Construction Injection

ii. Setter Injection

Q6). Can you differentiate between ApplicationContext and BeanFactory in Spring?

  • 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 and 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.

Q7). How is the configuration metadata provided to the Spring container?

There are three ways in which the configuration metadata is provided to the Spring container, enumerated as follow:

Annotation-based Configuration - By default, annotation wiring is turned off in the Spring container. Using annotations on the application 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 the Spring Framework. It has two important components:

i. @Bean annotation- Same as that if the <bean/> element

ii. @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.

Q8. What do you understand by Spring Beans? How many bean scopes are supported by Spring Framework?

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.
Q9). Please explain the Bean lifecycle in Spring Bean Factory Container?
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 beans definition, Spring then populated 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 then the setBeanFactiry() method is called by passing the bean ID.
5.If there are any BeanPostProcessors associated with the bean then the preProcessBeforeInitialization() methods are called.
6. If an init-method is specified for the bean, then it will be called.
7. At Last, the postProcessAfterInitialization() methods are called, if there are any BeanPostProcessors associated with the bean.

Q10). What is Annotation-based container configuration? Also, explain how to turn on annotation wiring in Spring Framework.
  • 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.
Q11). Please explain the various annotations supported by Spring.
  • @Autowired - Used to 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 makes 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 BeanInitializerException 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.
Q12). What do you mean by Spring DAO support?
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.

Q13). What classes does the JDBC API contain?
  • JdbcTemplate
  • NamedParameterJdbcTemplate
  • SimpleJdbcCall
  • SimpleJdbcInsert
  • SimpleJdbcTemplate
Q14). How will you access Hibernate using the Spring Framework?
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.

Q15). Enumerate the types of transaction management supported by Spring?
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.
Q16). Please explain the AOP technique.
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.

Q17). What is Advice in Spring? Explain its various types.
Any action taken by an aspect at some particular jointpoint in Spring Framework is called an Advice. Spring AOP makes use of advice can be of the following types:
1. After(finally) - Configured using the @After annotation mark. it is executed after a jointpoint method, whether exiting normally or throwing an exception.
2. After returning - Configured using the @AfterReturning annotation mark, it is executed right after the jointpoint 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 jointpoint method.
5. Before - Configured using the @Before annotation mark, it is executed before the jointpoint method.

Q18. Could you draw a comparison between concern and cross-cutting concerns in Spring AOP?
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 applied throughout the entire Spring application.

Q19). What do you understand by the Spring MVC Framework?
  • 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.
Q20). Please explain DispatcherServlet.
  • The Dispatcher Servlet is the essence of the Spring Web MVC framework and handles all the HTTP requests as well as responses. Upon receiving the entry of handler mapping from the configurations file, the DispatcherServlet forwards the request to the controller.
  • Thereafter, the controller returns an object of Model and View. Afterward, the DispatcherServlet checks the configuration file for the entry of view resolver and calls the specified view component.
Q21). What is Maven Dependency?
Maven is a project build management software, it means it lets you define your project dependencies, features, and behaviors. To do this, Maven downloads plugins and dependencies for various online repositories.

  
Previous
Next Post »

1 comments:

Click here for comments
Unknown
admin
8 April 2020 at 19:32 ×

Good content.
Add topics also

Reply
avatar