Spring Interview Questions
Spring Interview Questions
Spring Interview Questions
http://www.tutorialspoint.com/spring/spring_interview_questions.htm
Copyright tutorialspoint.com
Dear readers, these Spring Interview Questions have been designed specially to get you
acquainted with the nature of questions you may encounter during your interview for the subject
of Spring. As per my experience good interviewers hardly plan to ask any particular question
during your interview, normally questions start with some basic concept of the subject and later
they continue based on further discussion and what you answer:
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 J2EE
development easier to use and promote good programming practice by enabling a POJO-based
programming model.
What are benefits of using spring?
Following is the list of few of the great benefits of using Spring Framework:
Lightweight: Spring is lightweight when it comes to size and transparency. The basic
version of spring framework is around 2MB.
Inversion of control IOC: Loose coupling is achieved in spring using the technique
Inversion of Control. The objects give their dependencies instead of creating or looking for
dependent objects.
Aspect oriented AOP: Spring supports Aspect oriented programming and enables cohesive
development by separating 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 such as Struts or other over engineered or
less popular web frameworks.
Transaction Management: Spring provides a consistent transaction management
interface that can scale down to a local transaction usingasingledatabase, forexample and scale up
to global transactions usingJTA, forexample.
Exception Handling: Spring provides a convenient API to translate technology-specific
exceptions thrownbyJDBC, Hibernate, orJDO, forexample into consistent, unchecked exceptions.
What are the different modules in Spring framework?
Following are the modules of the Spring framework:
Core module
Bean module
Context module
Expression Language module
JDBC module
ORM module
OXM module
Java Messaging ServiceJMS module
Transaction module
Web module
Web-Servlet module
Web-Struts module
Web-Portlet module
What is Spring configuration file?
Spring configuration file is an XML file. This file contains the classes information and describes how
these classes are configured and introduced to each other.
What is Dependency Injection?
Inversion of Control IoC is a general concept, and it can be expressed in many different ways and
Dependency Injection is merely one concrete example of Inversion of Control.
This concept says that you do not create your objects but describe how they should be created.
You don't directly connect your components and services together in code but describe which
services are needed by which components in a configuration file. A container theIOCcontainer is then
responsible for hooking it all up.
What are the different types of IoC dependencyinjection?
Types of IoC are:
Constructor-based dependency injection: Constructor-based DI is accomplished when
the container invokes a class constructor with a number of arguments, each representing a
dependency on other class.
Setter-based dependency injection: Setter-based DI is accomplished by the container
calling setter methods on your beans after invoking a no-argument constructor or noargument static factory method to instantiate your bean.
Which DI would you suggest Constructor-based or setter-based DI?
Since you can mix both, Constructor- and Setter-based DI, it is a good rule of thumb to use
constructor arguments for mandatory dependencies and setters for optional dependencies. Note
that the use of a @Required annotation on a setter can be used to make setters required
dependencies.
What are the benefits of IOC?
The main benefits of IOC or dependency injection are:
It minimizes the amount of code in your application.
It makes your application easy to test as it doesn't require any singletons or JNDI lookup
mechanisms in your unit test cases.
Loose coupling is promoted with minimal effort and least intrusive mechanism.
IOC containers support eager instantiation and lazy loading of services.
What is 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. The core construct of AOP is the
aspect, which encapsulates behaviors affecting multiple classes into reusable modules.
What is Spring IoC container?
The Spring IoC creates the objects, wire them together, configure them, and manage their
complete lifecycle from creation till destruction. The Spring container uses dependency injection
file.
byType: Autowiring by property datatype. Spring container looks at the properties of the
beans on which autowire attribute is set to byType in the XML configuration file. It then tries
to match and wire a property if its type matches with exactly one of the beans name in
configuration file. If more than one such beans exist, a fatal exception is thrown.
constructor: Similar to byType, but type applies to constructor arguments. If there is not
exactly one bean of the constructor argument type in the container, a fatal error is raised.
autodetect: Spring first tries to wire using autowire by constructor, if it does not work, Spring
tries to autowire by byType.
What are the limitations with autowiring?
Limitations of autowiring are:
Overriding possibility: You can still specify dependencies using <constructor-arg> and
<property> settings which will always override autowiring.
Primitive data types: You cannot autowire so-called simple properties such as primitives,
Strings, and Classes.
Confusing nature: Autowiring is less exact than explicit wiring, so if possible prefer using
explicit wiring.
Can you inject null and empty string values in Spring?
Yes.
What is Annotation-based container configuration?
An alternative to XML setups is provided by annotation-based configuration which relies on the
bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of
using XML to describe a bean wiring, the developer moves the configuration into the component
class itself by using annotations on the relevant class, method, or field declaration.
How do you turn on annotation wiring?
Annotation wiring is not turned on in the Spring container by default. So, before we can use
annotation-based wiring, we will need to enable it in our Spring configuration file by configuring
<context:annotation-config/>.
What does @Required annotation mean?
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 through autowiring.
The container throws BeanInitializationException if the affected bean property has not been
populated.
What does @Autowired annotation mean?
This annotation provides more fine-grained control over where and how autowiring should be
accomplished. The @Autowired annotation can be used to autowire bean on the setter method just
like @Required annotation, constructor, a property or methods with arbitrary names and/or
multiple arguments.
What does @Qualifier annotation mean?
There may be a situation when you create more than one bean of the same type and want to wire
only one of them with a property, in such case you can use @Qualifier annotation along with
@Autowired to remove the confusion by specifying which exact bean will be wired.
What are the JSR-250 Annotations? Explain them.
Spring has JSR-250 based annotations which include @PostConstruct, @PreDestroy and @Resource
annotations.
What is Advice?
This is the actual action to be taken either before or after the method execution. This is actual
piece of code that is invoked during program execution by Spring AOP framework.
What is Pointcut?
This is a set of one or more joinpoints where an advice should be executed. You can specify
pointcuts using expressions or patterns as we will see in our AOP examples.
What is Introduction?
An introduction allows you to add new methods or attributes to existing classes.
What is Target object?
The object being advised by one or more aspects, this object will always be a proxy object. Also
referred to as the advised object.
What is Weaving?
Weaving is the process of linking aspects with other application types or objects to create an
advised object.
What are the different points where weaving can be applied?
Weaving can be done at compile time, load time, or at runtime.
What are the types of advice?
Spring aspects can work with five kinds of advice mentioned below:
before: Run advice before the a method execution.
after: Run advice after the a method execution regardless of its outcome.
after-returning: Run advice after the a method execution only if method completes
successfully.
after-throwing: Run advice after the a method execution only if method exits by throwing
an exception.
around: Run advice before and after the advised method is invoked.
What is XML Schema based aspect implementation?
Aspects are implemented using regular classes along with XML based configuration.
What is @AspectJ? based aspect implementation?
@AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5
annotations.
How JDBC can be used more efficiently in spring framework?
JDBC can be used more efficiently with the help of a template class provided by spring framework
called as JdbcTemplate.
How JdbcTemplate can be used?
With use of Spring JDBC framework the burden of resource management and error handling is
reduced a lot. So it leaves developers to write the statements and queries to get the data to and
from the database. JdbcTemplate 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.
What are the types of the transaction management Spring supports?
interface. Controllers interpret user input and transform it into a model that is represented to the
user by the view. Spring implements a controller in a very abstract way, which enables you to
create a wide variety of controllers.
Explain the @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.
Explain @RequestMapping annotation.
@RequestMapping annotation is used to map a URL to either an entire class or a particular handler
method.
What are the ways to access Hibernate by using Spring?
There are two ways to access hibernate using spring:
Inversion of Control with a Hibernate Template and Callback.
Extending HibernateDAOSupport and Applying an AOP Interceptor node.
What are ORM's Spring supports ?
Spring supports the following ORM's :
Hibernate
iBatis
JPA Java Persistence API
TopLink
JDO Java Data Objects
OJB
What is Next ?
Further you can go through your past assignments you have done with the subject and make sure
you are able to speak confidently on them. If you are fresher then interviewer does not expect you
will answer very complex questions, rather you have to make your basics concepts very strong.
Second it really doesn't matter much if you could not answer few questions but it matters that
whatever you answered, you must have answered with confidence. So just feel confident during
your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very
best for your future endeavor. Cheers :-)
Processing math: 78%