Spring Interview Questions


Question: Why Spring framework ?
Or
What are the benefits of Spring Framework?
Or
What are the advantages of using Spring Framework ?
  Question: What is the idea of using Spring instead?  
Question:  What are the different types of modules in Spring?
or
On what modules Spring framework is based?
Question:  What is new in Spring 2.5 as compared to 2.0?  
Question:  What is the significance of BeanFactory interface?
Or
What is the use of BeanFactory interface?
Question:  What is the significance of ApplicationContext?
Or
What is the use of ApplicationContext?
Question:  Difference between BeanFactory and ApplicationContext?
Question: Which is more used BeanFactory or ApplicationContext?
Or
Which one you prefer BeanFactory or ApplicationContext?
Question:  What are Lazily-instantiated beans?
Question:  What is XmlBeanFactory ?
Question:  What are the common implementations of the Application Context ?
Question:  Does Spring Framework support Transaction context propagation across remote calls ?
Question: What will be the outcome of using FileSystemXmlApplicationContext or ClassPathXmlApplicationContext class for request, session and global session scopes for beans defined in the Spring's Application Context?
Question: How does spring implementation look like?

Question:

What is the typical Bean life cycle in Spring Bean Factory Container ?

Q:

Why Spring framework ?
Or
What are the benefits of Spring Framework?
Or
What are the advantages of using Spring Framework ?

A:
  • Because Spring does not use much memory and CPU for loading beans, It is Comparatively light weight container in comparison to other J2EE containers.
  • Provides services like transaction control, AOP management, JDBC interaction.
  • Spring Helps in creating loosely coupled applications.
  • It is not dependent on any Application servers.
  • Through aspect oriented programming logging, transaction, security etc. set of activities can be easily managed.
  • Supports for data access techniques such as DAO, JDBC, Hibernate, IBATIS, JDO etc.
  • Spring configuration is done in standard XML format which easy to write and understand.
 

Q:

What is the idea of using Spring instead?

A:
  • Spring works on IOC (inversion of control) or DI (dependency injection) design pattern.
  • Spring provides container to create and manage objects and also provides enterprise services to those objects.
  • Spring is an open source application development framework.
  • It provides components for different tiers of application e.g. web, middle/business and data access.
 

Q:

What are the different types of modules in Spring?
or
On what modules Spring framework is based?

A: Spring has seven different modules to cater an enterprise application. These are :
  • Core container: BeanFactory, an implementation of the Factory pattern is an essential component of core container. The BeanFactory applies the Inversion of Control (IOC) pattern to separate an application's configuration and dependency specification from the actual application code.
  • Spring context: Spring context comprise of configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as internalization, validation, JNDI, and scheduling functionality.
  • Spring AOP: Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework, through its configuration management feature. Through AOP services like transaction control, logging etc can be easily managed.
  • Spring DAO: Spring DAO makes it easy to work with relational database management systems on the Java platform using JDBC and object-relational mapping tools. Spring JDBC DAO abstraction layer offers a meaningful exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections.
  • Spring ORM: The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps.
  • Spring Web module: Built on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.
  • Spring MVC framework: Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and assembles various view technologies including JSP, Tiles, iText, and POI.
 

Q:

What is new in Spring 2.5 as compared to 2.0?

A: Spring 2.5 comes with some new and some enhanced features. Such as:
  • IOC container: New bean scopes, easier xml configuration, extensible xml authoring, annotations.
  • AOP: Easier xml configuration, support for @AspectJ aspects, support for bean name pointcut element, support for AspectJ load-time waving.
  • Middle tier: Declarative transactions in xml, full Websphere transaction management support, JPA, Asynchronous JMS, JDBC improvements.
  • Web tier: Changes in Spring MVC, Portlet framework, Tiles, JSF, JAX-WS support, etc.
 

Q:

What is the significance of BeanFactory interface?
Or
What is the use of BeanFactory interface?

A: BeanFactory helps in creating Spring object with some basic functionality around object management through a well defined configuration framework.
  BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml")); 
 

Q:

What is the significance of ApplicationContext?
Or
What is the use of ApplicationContext?

A: ApplicationContext basically revolves around Spring’s BeanFactory and provides enterprise features e.g. message resourcing, event propagating, application-layer-specific contexts to applications, etc. It is derived from BeanFactory, so has all the functionality of BeanFactory and adds lots more.
 

Q:

Difference between BeanFactory and ApplicationContext?

A:

BeanFactory represents core configuration which provides basic functionality while ApplicationContext is more about enterprise-centric functionality support.

BeanFactory ApplicationContext

Instantiates beans lazily i.e. Beans are instantiated when you want them from BeanFactory, not on loading
// loading bean factory
 BeanFactory factory = new XMLBeanFactory(new ClassPathResource(“spring-beans.xml”));
// instantiating bean from factory or get bean

 Emp emp = factory.getBean(”myBean”);

Beans are instantiated when ApplicationContext is loaded/up.
// Loading Application Context and instantiating bean
 ApplicationContext appCtx = new ClassPathXmlApplicationContext(“spring-beans.xml”);
No support for Message Internationalization Supports Message Internationalization
No event handling or event propagation mechanism. Provides event-propagation to the beans which are participating in listening to ApplicationContext i.e. the beans implementing ApplicationListener interface.
Access to low level resources is not convenient in BeanFactory.

Access is very convenient. Application contexts has a generic/common way to load file resources. It is a resource loader, can load resources including
- FileSystemResource
- ClassPathResource
- ServletContextResource
- InputStreamResource

 

Q:

Which is more used BeanFactory or ApplicationContext?
Or
Which one you prefer BeanFactory or ApplicationContext?

A: ApplicationContext. It covers almost all features which are there in BeanFactory and in addition also provides enterprise related features, which your application might require in future.
 

Q:

What are Lazily-instantiated beans?

A: Spring by default instantiate all beans at startup. This helps in overcoming any problems which came across while instantiating beans at startup only. But sometimes, some beans are not required to be initialized during startup, rather we want them to be initialized in later stages of application. For such beans, we include “lazy-init="true"” while configuring beans.
<bean lazy-init="true">
    <!-- this bean will not be pre-instantiated... -->
</bean>
 

Q:

What is XmlBeanFactory ?

A: XmlBeanFactory class is one of the many implementations of BeanFactory interface. This implementation helps to define your application objects and dependencies between such objects, in terms of XML format. XmlBeanFactory extends DefaultListableBeanFactory and defines two constructors.
 

Q:

What are the common implementations of the Application Context ?

A:
There are three most commonly used implementation of ‘Application Context’ as:

ClassPathXmlApplicationContext : It loads the context definition from an XML file present in the classpath, treating context definitions as classpath resources. File ‘beans.xml’ should be present in classpath of application.
  ApplicationContext appContext = new ClassPathXmlApplicationContext(“beans.xml”);

FileSystemXmlApplicationContext : It loads the context definition from an XML file present in the filesystem. Need to give absolute path of the file ‘beans.xml’ as given below :
ApplicationContext appContext = new   FileSystemXmlApplicationContext(“F:/java_program/SpringProject/src/com/thecafetechno/beans.xml”);

XmlWebApplicationContext : It loads the context definition from an XML file present within a web application.
 

Q:

Does Spring Framework support Transaction context propagation across remote calls ?

A: No
 

Q:

What will be the outcome of using FileSystemXmlApplicationContext or ClassPathXmlApplicationContext class for request, session and global session scopes for beans defined in the Spring's Application Context?

A: There will be an exception IllegalStateException thrown as the bean scope is not known for these implementation of Application context from SpringFramework.
 

Q:

How does spring implementation look like?

A: For a typical Spring Application we need the following files:
  • An interface that defines the functions.
  • An Implementation that contains properties, its setter and getter methods, functions etc.,
  • A XML file called Spring configuration file.
  • Client program that uses the function.
 

Q:

What is the typical Bean life cycle in Spring Bean Factory Container ?

A: Bean life cycle in Spring Bean Factory Container is as follows:
  • The spring container finds the bean’s definition from the XML file and instantiates the bean.
  • Using the dependency injection, spring populates all of the properties as specified in the bean definition.
  • If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
  • If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
  • If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
  • If an init-method is specified for the bean, it will be called.
  • Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.



Spring Interview Questions

Similar Topics
Forum For Java Interview Questions new

Java Interview Questions
Java Collection Interview Questions
JSP Interview Questions
Servlet Interview Questions
EJB Interview Questions
JMS Interview Questions
Struts Interview Questions
Spring Interview Questions
WMB Interview Questions
DB Interview Questions

Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions