Spring framework is an open source application, used to develop Java applications easily and rapidly. There are also extensions available to build web applications in other than Java EE platform. It is designed for Java programmers and helps them using a POJO-based programming model.
Spring provides various features that make it so useful IDE for developers.
- Lightweight: Spring does not consume or use much memory while loading beans, providing services like transaction control, Aspect Oriented Programming (AOP) management, JDBC interaction. It is comparatively lightweight container than any other J2EE containers.
- Inversion of Control (IoC): Spring provide IoC container that manages Java objects from instantiation to destruction. The IoC container leaves Java components loosely coupled and allow the programmer to code to abstractions.
- Aspect Oriented Programming (AOP): Spring provides AOP unit of modularity called Aspect. It helps separating business logic from system services. The modular approach refers to breaking of program logic into distinct parts called concerns.
- MVC Framework: Spring provides model-view-controller (MVC) architecture that can be used to develop flexible Web applications.
- Model: The Model consists of POJO and it encapsulates application data.
- View: The View generates HTML output by rendering the model data.
- Controller: The Controller processes user requests and build appropriate model. It passes it to the view for rendering.
- Transaction Management: Spring provides different transaction management APIs. It supports both programmatic as well as declarative transaction management. Spring transaction management can be implemented without an application server.
- Exception Handling: Spring provides APIs to translate specific exceptions thrown by other frameworks or modules such as Hibernate, JDBC or JDO into consistent and unchecked exceptions.
Spring is modular enables you to select the applicable modules without installing all. It provides different modules that can be used in Web application development.
The modules available in Spring are depicted in the diagram below:
Test Module in Spring
The test module provides support of Spring components testing with JUnit or TestNG frameworks.
Core Container in Spring
The core container includes Beans, Core, Context and Expression Language components.
The “Core” component provides the IoC and Dependency Injection features of Spring.
The “Bean” component contains BeanFactory that which instantiates, configures, and manages a number of beans. This helps processing “Dependency Injection” of core.
The “Context” component is medium to access the objects defined and configured. It is built on the base provided by core and beans. The ApplicationContext interface is the central point of the Context component.
The “Expression Language” component provides a language for manipulating objects at run-time.
The “Aspect Oriented Programming (AOP)” module is used to define method-interceptors to cleanly decouple code that implements functionality that should be separated.
The “Aspects” module provides integration with AspectJ. Aspects in Spring are defined in two ways: one is schema based and the other is annotation based. Aspects contain pointcut and advice body.
The “Instrumentation” module exposes performance and resource utilization metrics for the Spring container and gives you runtime control of the container.
Spring Data Access/Integration
The data access/integration module consists of JDBC, ORM, OXM, JMS and Transaction components.
The “JDBC” component provides a JDBC-abstraction layer that helps to reduce the tasks of JDBC relating coding.
The “ORM” layer provides object-relational mapping APIs including JPA, JDO, Hibernate, and iBatis.
The “OXM” component support object-XML mapping implementations. This module is extensible and provides integrations with popular frameworks like Castor, JAXB, XmlBeans and XStream.
The “Java Messaging Service (JMS)” component is used to send messages and also having a JMS listener process the messages sent.
The “Transaction” component is used to perform database operation and its management. It supports local transactions as well as global transactions.
Spring Web MVC
The Web MVC module consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet components.
The “Web” component is also called Spring Web Flow that integrates with existing components like Spring MVC and JSF, in both Servlet and Portlet environments. The SWF module is best suited for building web application modules with controlled navigations.
The “Web-Servlet” component allows you to communicate via DispatcherServlet. When the HTTP request is sent, the DispatcherServlet calls corresponding controller. The controller renders the JSP and sends HTTP response.
The “Web-Portlet” component handles two phases of portlet requests. The action request and the render request. It provides various controllers that already have many functionality to use.
The “Web Struts” component is a framework that is used to create web applications. It can be integrated with Spring Web module and used with other Struts extensions that use a custom RequestProcessor. It also can create new Actions for every request.
Spring Interview Questions and Answers
- What is the use of the BeanFactory interface in Spring?
The BeanFactory interface is used to access Spring container. The Spring containers such as ApplicationContext, ListableBeanFactory, ConfigurableBeanFactory are built upon the BeanFactory interface.
The interface provides basic functionality listed below:
- The BeanFactory interface loads the beans definitions and their property descriptions from the configuration source such as XML configuration file.
- The BeanFactory interface instantiates the beans when they are requested. It is able to create associations between collaborating objects as beans are instantiated.
For Example: The following statement loads your configuration file from the root of the classpath:
Resource resource = new ClassPathResource(“beans.xml”);
Next, you can use the following statement to instantiate a bean factory by passing in a Resource object with the configuration file loaded:
BeanFactory factory = new XmlBeanFactory(resource);
- The BeanFactory interface participates in bean life cycle by making calls to custom initialization and destruction methods.
- What is the significance of ApplicationContext in Spring?
The ApplicationContext is sub-interface of BeanFactory. The application context provides more advanced features than the bean factory and at the same time, it keeps the basic features compatible.
Spring provides multiple classes that implement this interface and helps developers use configuration information in applications.
To access ApplicationContext, it is required to implement the ApplicationContextAware interface in the respective java bean.
It has a method and it will invoke this method and pass the concrete object for AppplicationContext. You can get access to all the configuration information using this.
void setApplicationContext(ApplicationContext applicationContext)throws BeansException
- What is the difference between BeanFactory and ApplicationContext in Spring?
The BeanFactory and ApplicationContext, both are interfaces. ApplicationContext offers much more than BeanFactory.
- ApplicationContext provides support for i18n for text messages.
- ApplicationContext provides a generic way to load file resources, such as images.
- ApplicationContext publishes events to beans that are registered as listeners.
- ApplicationContext acts as a ResourceLoader that provides an application with access to deployment-specific Resource instances. This is used for handling low-level resources.
- ApplicationContext also implements MessageSource, an interface used to obtain localized messages.
Thus, there are differences between the BeanFactory and ApplicationContext interfaces.
- What are the common implementations of the ApplicationContext in Spring?
There are many implementations of the ApplicationContext interface. Common ones are:
- ClassPathXmlApplicationContext: This implementation loads bean definitions from XML files location in classpath.
ApplicationContext context = new ClassPathXmlApplicationContext(“myconfig.xml”);
- FileSystemXmlApplicationContext: This implementation loads bean definitions from XML files in the file system.
ApplicationContext context = new FileSystemXmlApplicationContext(“c:/myconfig.xml”);
- XmlWebApplicationContext: This implementation is used to represent Spring Container for web applications.
- What are singleton beans and how can you create prototype beans in Spring?
There is an attribute, named “singleton” in the bean tag. If its property is set to true, then it becomes singleton and if its property is set to false, then the bean becomes prototype bean. By default, the bean attribute is always set to true. It means the he beans in the Spring framework are by default singleton beans.
<bean id=’mybean’ class=’com.act.MyApp’
- What are the different types of bean injections in Spring?
There are two types of bean injections in Spring. They are:
- By setter
- By constructor
- How to integrate Java Server Faces (JSF) with Spring?
By declaring JSF managed-beans in the “faces-config.xml” configuration file, you allow the FacesServlet to instantiate that bean at startup. We can integrate JSF and Spring in two ways.
- DelegatingVariableResolver: This JSF variable resolver is available within Spring and lets you use JSF and Spring together.
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
This JSF variable resolver will first delegate value lookups to the default resolver of JSF implementation and then Spring’s WebApplicationContext. It enables you to easily inject dependencies into one’s JSF-managed beans.
- FacesContextUtils: There are times when you need to grab a bean explicitly. The can be done easily by the FacesContextUtils. It is similar to WebApplicationContextUtils, except that it takes a FacesContext parameter rather than a ServletContext
ApplicationContext context = FacesContextUtils.getWebApplicationContext