Spring Interview Questions: Q. What Is Spring?
Spring Interview Questions: Q. What Is Spring?
Q. 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 application,
but there are extensions for building web applications on top of the Java EE
platform. Spring framework targets to make Java EE development easier to use and
promote good programming practice by enabling a POJO-based programming
model.
Exception Handling:Spring provides a convenient API to translate technologyspecific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent,
unchecked exceptions.
Core module
Bean module
Context module
JDBC module
ORM module
OXM module
Transaction module
Web module
Web-Servlet module
Web-Struts module
Web-Portlet module
Q. XMLBeanFactory
The most useful one is org.springframework.beans.factory.xml.XmlBeanFactory,
which loads its beans based on the definitions contained in an XML file. This
container reads the configuration metadata from an XML file and uses it to create a
fully configured system or application.
The implementation that contains properties, its setter and getter methods,
functions etc.,
Spring AOP
You can use both Constructor-based and Setter-based Dependency Injection. The
best solution is using constructor arguments for mandatory dependencies and
setters for optional dependencies.
Annotation-based configuration
Java-based configuration
singleton scope, Spring scopes the bean definition to a single instance per
Spring IoC container.
prototype scope, a single bean definition has any number of object instances.
request scope, a bean is defined to an HTTP request. This scope is valid only in
a web-aware Spring ApplicationContext.
session scope, a bean definition is scoped to an HTTP session. This scope is also
valid only in a web-aware Spring ApplicationContext.
The spring container finds the beans definition from the XML file and
instantiates the bean.
Spring populates all of the properties as specified in the bean definition (DI).
bean definition, in order to define the so-called inner bean. Inner beans are always
anonymous and they are always scoped as prototypes.
The<list> type is used for injecting a list of values, in the case that duplicates
are allowed.
The<set> type is used for wiring a set of values but without any duplicates.
The autowiring functionality has five modes which can be used to instruct Spring
container to use autowiring for dependency injection:
no:This is default setting. Explicit bean reference should be used for wiring.
autodetect: Spring first tries to wire using autowire by constructor, if it does not
work, Spring tries to autowire bybyType.
Overriding: You can still specify dependencies using<constructorarg> and <property> settings which will always override autowiring.
Q. @Required annotation
This annotation simply indicates that the affected bean property must be populated
at configuration time, through an explicit property value in a bean definition or
Q. @Autowired annotation
The @Autowired annotation provides more fine-grained control over where and how
autowiring should be accomplished. It can be used to autowire bean on the setter
method just like @Required annotation, on the constructor, on a property or pn
methods with arbitrary names and/or multiple arguments.
Q. @Qualifier annotation
When there are more than one beans of the same type and only one is needed to be
wired with a property, the@Qualifier annotation is used along
with @Autowired annotation to remove the confusion by specifying which exact
bean will be wired.
Q. JdbcTemplate
JdbcTemplate class provides many convenience methods for doing things such as
converting database data into primitives or objects, executing prepared and callable
statements, and providing custom database error handling.
Hibernate
iBatis
TopLink
OJB
Q. Explain AOP
Aspect-oriented programming, or AOP, is a programming technique that allows
programmers to modularize crosscutting concerns, or behavior that cuts across the
typical divisions of responsibility, such as logging and transaction management.
Q. Aspect
The core construct of AOP is the aspect, which encapsulates behaviors affecting
multiple classes into reusable modules. It ia a module which has a set of APIs
providing cross-cutting requirements. For example, a logging module would be
called AOP aspect for logging. An application can have any number of aspects
depending on the requirement. In Spring AOP, aspects are implemented using
regular classes annotated with the @Aspect annotation (@AspectJ style).
data transfer are the concerns which are needed in almost every module of an
application, hence they are cross-cutting concerns.
Q. Join point
The join point represents a point in an application where we can plug-in an AOP
aspect. It is the actual place in the application where an action will be taken using
Spring AOP framework.
Q. Advice
The advice is the actual action that will be taken either before or after the method
execution. This is actual piece of code that is invoked during the program execution
by the Spring AOP framework.
Spring aspects can work with five kinds of advice:
Q. Pointcut
The pointcut is a set of one or more joinpoints where an advice should be executed.
You can specify pointcuts using expressions or patterns.
Q. What is Introduction?
An Introduction allows us to add new methods or attributes to existing classes.
Q. What is a Proxy?
A proxy is an object that is created after applying advice to a target object. When
you think of client objects the target object and the proxy object are the same.
BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator
Metadata autoproxying
In this implementation case, aspects are implemented using regular classes along
with XML based configuration.
Q. DispatcherServlet
The Spring Web MVC framework is designed around a DispatcherServlet that
handles all the HTTP requests and responses.
Q. WebApplicationContext
The WebApplicationContext is an extension of the plain ApplicationContext that has
some extra features necessary for web applications. It differs from a
normal ApplicationContext in that it is capable of resolving themes, and that it
knows which servlet it is associated with.
Q. @Controller annotation
The @Controller annotation indicates that a particular class serves the role of a
controller. Spring does not require you to extend any controller base class or
reference the Servlet API.
Q. @RequestMapping annotation
@RequestMapping annotation is used to map a URL to either an entire class or a
particular handler method.