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.
- Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of the spring framework is around 2MB.
- 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.
- Aspect-oriented (AOP): Spring
supports Aspect-oriented programming and separates application business logic
from system services.
- Container: Spring contains and manages
the life cycle and configuration of application objects.
- MVC Framework: Spring’s
web framework is a well-designed web MVC framework, which provides a great
alternative to web frameworks.
- Transaction Management: Spring
provides a consistent transaction management interface that can scale down to a
local transaction and scale up to global transactions (JTA).
- Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.
- 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.
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
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.
- 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.
- @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.
- JdbcTemplate
- NamedParameterJdbcTemplate
- SimpleJdbcCall
- SimpleJdbcInsert
- SimpleJdbcTemplate
- 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.
- 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.
- 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.
1 comments:
Click here for commentsGood content.
ReplyAdd topics also
ConversionConversion EmoticonEmoticon