What Are The Major Features in Different Versions of Spring Framework?
What Are The Major Features in Different Versions of Spring Framework?
5. How many modules are there in Spring Framework and what are they?
There are around 20 modules which are generalized into Core Container, Data Access/Integration, Web,
AOP (Aspect Oriented Programming), Instrumentation and Test.
Spring Core Container – This layer is basically the core of Spring Framework. It contains the
following modules :
a. Spring Core
b. Spring Bean
c. SpEL (Spring Expression Language)
d. Spring Context
Data Access/Integration – This layer provides support to interact with the database. It contains
the following modules :
a. JDBC (Java DataBase Connectivity)
b. ORM (Object Relational Mapping)
c. OXM (Object XML Mappers)
d. JMS (Java Messaging Service)
e. Transaction
Web – This layer provides support to create web application. It contains the following modules
:
a. Web
b. Web – MVC
c. Web – Socket
d. Web – Portlet
Aspect Oriented Programming (AOP) – In this layer you can use Advices, Pointcuts etc., to
decouple the code.
Instrumentation – This layer provides support to class instrumentation and classloader
implementations.
Test – This layer provides support to testing with JUnit and TestNG.
Few Miscellaneous modules are given below:
Messaging – This module provides support for STOMP. It also supports an annotation
programming model that is used for routing and processing STOMP messages from WebSocket
clients.
Aspects – This module provides support to integration with AspectJ.
At the core of the Spring Framework, lies the Spring container. The container creates the object, wires
them together, configures them and manages their complete life cycle. The Spring container makes use
of Dependency Injection to manage the components that make up an application. The container
receives instructions for which objects to instantiate, configure, and assemble by reading the
configuration metadata provided. This metadata can be provided either by XML, Java annotations or
Java code.
19. What is the Bean life cycle in Spring Bean Factory Container?
Bean life cycle in Spring Bean Factory Container is as follows:
1. The Spring container instantiates the bean from the bean’s definition in the XML file.
2. Spring populates all of the properties using the dependency injection, as specified in the bean
definition.
3. The factory calls setBeanName() by passing the bean’s ID, if the bean implements the
BeanNameAware interface.
4. The factory calls setBeanFactory() by passing an instance of itself, if the bean implements the
BeanFactoryAware interface.
5. preProcessBeforeInitialization() methods are called if there are any BeanPostProcessors
associated with the bean.
6. If an init-method is specified for the bean, then it will be called.
7. Finally, postProcessAfterInitialization() methods will be called if there are any
BeanPostProcessors associated with the bean.
To understand it in better way check the below diagram:
20. Explain inner beans in Spring.
A bean can be declared as an inner bean only when it is used as a property of another bean. For
defining a bean, the Spring’s XML based configuration metadata provides the use of <bean> element
inside the <property> or <constructor-arg>. Inner beans are always anonymous and they are always
scoped as prototypes. For example, let’s say we have one Student class having reference
of Person class. Here we will be creating only one instance of Person class and use it inside Student.
Here’s a Student class followed by bean configuration file:
Student.java
1 public class Student
2 {
3 private Person person;
4 //Setters and Getters
5 }
6 public class Person
7 {
8 private String name;
9 private String address;
10 //Setters and Getters
11 }
studentbean.xml
1 <bean id=“StudentBean" class="com.edureka.Student">
2 <property name="person">
3 <!--This is inner bean -->
4 <bean class="com.edureka.Person">
5 <property name="name" value=“Scott"></property>
6 <property name="address" value=“Bangalore"></property>
7 </bean>
8 </property>
9 </bean>
21. Define Bean Wiring.
When beans are combined together within the Spring container, it’s called wiring or bean wiring. The
Spring container needs to know what beans are needed and how the container should use dependency
injection to tie the beans together, while wiring beans.
22. What do you understand by auto wiring and name the different modes of it?
The Spring container is able to autowire relationships between the collaborating beans. That is, it is
possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of
the BeanFactory.
Different modes of bean auto-wiring are:
a. no: This is default setting which means no autowiring. Explicit bean reference should be used
for wiring.
b. byName: It injects the object dependency according to name of the bean. It matches and wires
its properties with the beans defined by the same names in the XML file.
c. byType: It injects the object dependency according to type. It matches and wires a property if
its type matches with exactly one of the beans name in XML file.
d. constructor: It injects the dependency by calling the constructor of the class. It has a large
number of parameters.
e. autodetect: First the container tries to wire using autowire by constructor, if it can’t then it tries
to autowire by byType.
@Component: This marks a java class as a bean. It is a generic stereotype for any Spring-managed
component. The component-scanning mechanism of spring now can pick it up and pull it into the
application context.
@Controller: This marks a class as a Spring Web MVC controller. Beans marked with it are
automatically imported into the Dependency Injection container.
@Service: This annotation is a specialization of the component annotation. It doesn’t provide any
additional behavior over the @Component annotation. You can use @Service over @Component in
service-layer classes as it specifies intent in a better way.
@Repository: This annotation is a specialization of the @Component annotation with similar use and
functionality. It provides additional benefits specifically for DAOs. It imports the DAOs into the DI
container and makes the unchecked exceptions eligible for translation into Spring
DataAccessException.
34. What are the ways by which Hibernate can be accessed using Spring?
There are two ways by which we can access Hibernate using Spring:
a. Inversion of Control with a Hibernate Template and Callback
b. Extending HibernateDAOSupport and Applying an AOP Interceptor node
The
next section of Spring interview questions discusses on Spring AOP Interview Questions.
42. Point out the difference between concern and cross-cutting concern in Spring
AOP?
The concern is the behavior we want to have in a particular module of an application. It can be defined
as a functionality we want to implement.
The cross-cutting concern is a concern which is applicable throughout the application. This affects the
entire application. For example, logging, security and data transfer are the concerns needed in almost
every module of an application, thus they are the cross-cutting concerns.
43. What are the different AOP implementations?
Different AOP implementations are depicted by the below diagram:
44. What are the difference between Spring AOP and AspectJ AOP?
Spring AOP vs AspectJ AOP
Spring AOP AspectJ AOP
Runtime weaving through proxy is done Compile time weaving through AspectJ Java tools is done
It supports only method level PointCut It suports field level Pointcuts
It is DTD based It is schema based and Annotation configuration
45. What do you mean by Proxy in Spring Framework?
An object which is created after applying advice to a target object is known as a Proxy. In case of client
objects the target object and the proxy object are the same.
46. In Spring, what is Weaving?
The process of linking an aspect with other application types or objects to create an advised object is
called Weaving. In Spring AOP, weaving is performed at runtime. Refer the below diagram:
I hope this set of Spring Interview Questions and Answers will help you in preparing for your
interviews. All the best!