Spring Interview Questions

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Q1. What is Spring. ?

Ans. Spring is an open source framework. It provides libraries of classes which make it easier to develop tasks such as transaction management, database integration and web applications and create the help to address the complexity of development of enterprise application. The most important advantage of the Spring framework is its layered architecture, means allows you can select and work with any of its components.

Q 2. What are the benefits or advantages of Spring Framework?.

Ans. Below are the benefits or advantages of Spring:

  • Spring is a light weight container in comparison to J2EE containers.
  • Spring has layered architecture which means you can use what you need and leave what you don't need.
  • Spring helps creating a loosely coupled applications by using DI (Dependency Injection).
  • Spring has Predefined Templates
  • Spring is POJO-based, non-invasive framework which allows usage of its components. POJO enables continuous integration and testability.
  • Spring is not dependent on any Application servers.
  • Spring supports data access techniques such as DAO, JDBC, JDO, Hibernate, IBATIS, etc.
  • Application code is much easier to test using unit test
  • Spring support declarative transaction management, security and logging service like AOP
  • Spring support easy integration with third party tools and technologies.

Q3. What are different modules in spring?

Ans. Below are the modules of the Spring framework:

  • Core Container
  • Application context
  • AOP (Aspect Oriented Programming)
  • JDBC abstraction and DAO
  • ORM integration module (Object/Relational)
  • Web module
  • Servlet module
  • Struts module
  • MVC framework
  • Expression Language module
  • OXM module
  • Java Messaging Service(JMS) module
  • Transaction module

Q4. What is Spring configuration file?

Ans. 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 and filed under src/main/resources. During the build process, these are then copied into the WEB-INF/classes directory. In large project, you may have more than one Spring's bean configuration files then you put every single bean definition in a single file is hard to maintain and they may be stored in different file folder structures.

For example, you may have a Spring-Common.xml in common folder, a Spring-Connection.xml in connection folder, and a Spring-Security.xml security folder.

You can use the following code to load multiple Spring bean configuration files.

One way spring allows you to organize all Spring bean configuration files into a single XML file put this file under project classpath and import to other spring configuration file using import. Below is the Example of all configuration files in one xml file Spring-Module.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/
beans
http://www.springframework.org/schema/beans/
spring-beans-2.5.xsd">
<import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="moduleA/Spring-Security.xml"/>
</beans>

Now you can load this single Spring-Module.xml file like below

ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");

Or you can load all xml files like below

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml","Spring-Connection.xml",
"SpringSecurity-.xml"});

Q5. What is IOC container and DI?

Ans. The Inversion of Control (IoC) principle is implemented using the Dependency Injection (DI) design pattern and responsible to create the objects, configure the objects and wire them together and manages the java objects from creation to destruction through its BeanFactory and ApplicationContext containers

Below are the two types of IoC containers.

BeanFactory: BeanFactory provides basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. BeanFactory is just like a factory class that contains a collection of beans. The XmlBeanFactory is the implementation class for the BeanFactory interface. Using the BeanFactory, you need to create the instance of XmlBeanFactory class as given below.

Resource resource = new ClassPathResource("applicationContext.xml");  
BeanFactory factory = new XmlBeanFactory(resource);

In the constructor of XmlBeanFactory class you need to pass resource object to create the object of BeanFactory.

ApplicationContext: The ApplicationContext container includes all functionality of the BeanFactory container and adds more enterprise specific functionality such as the ability to resolve textual messages, including support for internationalization from a properties file, generic way to load file resources and the ability to publish application events to interested event listeners. The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface. This container is defined by the org.springframework.context.ApplicationContext interface. You need to instantiate the ClassPathXmlApplicationContext class to use the ApplicationContext as given below.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

In the constructor of ClassPathXmlApplicationContext class you need to pass name of the xml file to create the instance of ApplicationContext.

      Advertisements