Spring Interview questions
Spring Interview questions
ArrayList LinkedList
1) ArrayList internally uses dynamic array to store the LinkedList internally uses doubly linked list to store
elements. the elements.
2) Manipulation with ArrayList is slow because it Manipulation with LinkedList is faster than ArrayList
internally uses array. If any element is removed from because it uses doubly linked list so no bit shifting is
the array, all the bits are shifted in memory. required in memory.
3) ArrayList class can act as a list only because it LinkedList class can act as a list and queue both
implements List only. because it implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
HashMap HashTable
HashMap is not synchronized and therefore it is not thread HashTable is internally synchronized and therefore it is
safe. thread safe.
HashMap allows maximum one null key and any number HashTable doesn’t allow null keys and null values.
of null values.
Iterators returned by the HashMap are fail-fast in nature. Enumeration returned by the HashTable are fail-safe in
nature.
HashMap extends AbstractMap class. HashTable extends Dictionary class.
HashMap returns only iterators to traverse. HashTable returns both Iterator as well as Enumeration for
traversal.
HashMap is fast. HashTable is slow.
HashMap is not a legacy class. HashTable is a legacy class.
HashMap is preferred in single threaded applications. If Although HashTable is there to use in multi threaded
you want to use HashMap in multi threaded application, applications, now a days it is not at all preferred. Because,
wrap it using Collections.synchronizedMap() method. ConcurrentHashMap is better option than HashTable.
5) Both give constant time performance for insertion and retrieval operations.
Spring Framework
Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides
support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc.
The Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM, WEB MVC etc.
These are the design patterns that are used to remove dependency from the programming code. They make the code
easier to test and maintain.
The IoC container is responsible to instantiate (to instantiate the application class), configure(to configure the
object) and assemble(to assemble the dependencies between the objects) the objects. The IoC container gets
informations from the XML file and works accordingly.
IOC makes the code loosely coupled. In such case, there is no need to modify the code if our logic is moved to new
environment.
IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file
or annotation on configuration classes.
1) Predefined Templates
Spring framework provides templates for JDBC, Hibernate, JPA etc. technologies. So there is no need to write too
much code. It hides the basic steps of these technologies.
Let's take the example of JdbcTemplate, you don't need to write the code for exception handling, creating
connection, creating statement, committing transaction, closing connection etc. You need to write the code of
executing query only. Thus, it save a lot of JDBC code.
2) Loose Coupling
The Spring applications are loosely coupled because of dependency injection.
3) Easy to test
The Dependency Injection makes easier to test the application. The EJB or Struts application require server to run the
application but Spring framework doesn't require server.
4) Lightweight
Spring framework is lightweight because of its POJO implementation. The Spring Framework doesn't force the
programmer to inherit any class or implement any interface. That is why it is said non-invasive.
5) Fast Development
The Dependency Injection feature of Spring Framework and it support to various frameworks makes the easy
development of JavaEE application.
6) Powerful abstraction
It provides powerful abstraction to JavaEE specifications such as JMS, JDBC, JPA and JTA.
7) Declarative support
It provides declarative support for caching, validation, transactions and formatting.
The Spring framework comprises of many modules such as core, beans, context, expression language, AOP, Aspects,
Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts etc. These modules are grouped into
Test, Core Container, AOP, Aspects, Instrumentation, Data Access / Integration, Web (MVC / Remoting) .
TEST
Context
This module supports internationalization (I18N), EJB, JMS, Basic Remoting.
Expression Language
It is an extension to the EL defined in JSP. It provides support to setting and getting property values, method invocation,
accessing collections and indexers, named variables, logical and arithmetic operators, retrieval of objects by name etc
The instrumentation module provides support to class instrumentation and classloader implementations.
Web
This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet. These modules provide support to create web
application.
Easy to maintain and modify. By breaking your system into group of reusable objects or codes.
Spring framework to act as a central module to manage all the object dependencies easily and efficiently.
Spring Dependency Injection (DI) – Just change in Context xml bean property .
Responsible of IOC
Identify(Find) beans
1.BeanFactory
2.ApplicationContext
The Dependency Injection is a design pattern that removes the dependency of the programs or codes. In such case
we provide the information from the external source such as XML file. It makes our code loosely coupled and easier
for testing.
Two ways to perform Dependency Injection in Spring framework
Spring framework provides two ways to inject dependency
By Constructor
Auto Wiring enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.
Autowiring can't be used to inject primitive and string values. It works with reference only.
Advantage of Autowiring
It requires the less code because we don't need to write the code to inject the dependency explicitly
Disadvantage of Autowiring
No control of programmer.
Two of the most fundamental and important packages in Spring are the org.springframework.beans and
org.springframework.context packages. Code in these packages provides the basis for Spring's Inversion of
Control (alternately called Dependency Injection) features.
The ApplicationContext interface is built on top of the BeanFactory interface. It adds some extra functionality than
BeanFactory such as simple integration with Spring's AOP, message resource handling (for I18N), event propagation,
application layer specific context (e.g. WebApplicationContext) for web application. So it is better to use
ApplicationContext than BeanFactory.
BeanFactory
1) Bean instantiation/wiring
ApplicationContext
1) Bean instantiation/wiring
2) Springs AOP features
3) J2EE Supports
4) message resource handling (for use in internationalization)
5) Event Propagation (publish events to beans that are registered as listeners)
6.How is Spring’s singleton bean different from Gang of Four Singleton Pattern?
Gang of Four Singleton Pattern as having only one instance per ClassLoader whereas Spring Singleton is defined as
one instance of bean per container.
Factory – Spring uses factory pattern to create objects of beans using Application Context reference
// Spring uses factory pattern to create instances of the objects
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
Triangle triangle = (Triangle) factory.getBean("triangle");
triangle.draw();
Singleton – by default, beans defined in spring config file (xml) are only created once. No matter how many calls were
made using getBean() method, it will always have only one bean. This is because, by default all beans in spring are
singletons.
This can be overridden by using Prototype bean scope.Then spring will create a new bean object for every request.
Model View Controller – The advantage with Spring MVC is that your controllers are POJOs as opposed to being
servlets. This makes for easier testing of controllers. One thing to note is that the controller is only required to return a
logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers
for different view technologies.
Front Controller – Spring provides DispatcherServlet to ensure an incoming request gets dispatched to your controllers.
View Helper – Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from
presentation in views.
Template method – used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..).
For example JdbcTemplate, JmsTemplate, JpaTemplate.
Bean Lifecycle
life cycle of beans consist of call back methods which can be categorized broadly in two groups:
Scope Description
singleton Scopes a single bean definition to a single object instance per Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP
request request will have its own instance of a bean created off the back of a single bean definition. Only valid in
the context of a web-aware SpringApplicationContext.
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-
session
aware SpringApplicationContext.
global Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when
session used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
MICROSERVICE ARCHITECTURE