Spring Basics

 

What are we going to learn ?

 We will look at what exactly Spring is and what it provides. We will also look at Dependency Injection and Inversion Of control.

 
What is Spring ?
   Spring is an open source framework and Inversion Of Control container. Spring provides comprehensive infrastructure support for developing Java applications. JEE applications are complex and multi layered. Spring provides the capability to develop all layers of application using Spring API's. In contrast to single layered frameworks like Struts/hibernate, Spring is not invasive and not "all or nothing" framework. Spring acts like a pluggable framework where you can switch between different implementations. 
Typically, an application consists of components interacting and collaborating with one another. Spring provides you the capability to build applications from POJOs( Plain Old Java Objects ) and apply enterprise services non-invasively.
 
Dependency injection (DI) and Inversion Of Control(IOC):
 Dependency Injection is a software pattern that removes hard-coded dependencies. As explained earlier, applications involve collaboration between different components. 
Let's consider a small example to consider the advantages of using DI. Let's say you are creating a Student enrollment application and you would like to use log4j to log the functional behavior. Under normal circumstances, you create a logger class and use it for logging. Some of the disadvantages of the approach being 
  1. Hard coded reference to the log class.
  2. The main task at hand is to log the behavior but you are forced to handle boiler code to handle to the life cycle of the logger class(creating/initializing).
  3. Tightly couples your student class with the logger class thus making it necessary to change code in Student if you decide to use any other logging framework.
In DI, the onus of providing the reference of the required object/dependency and maintaining it's life cycle falls on the IOC Container. 
  1. Thus making the application lightly coupled. 
  2. The dependency is injected at either compile time or run time, thus offering flexibility.
  3. Removing boiler plate code of creating/initializing dependencies.
  4. Helpful for unit testing by injecting fake/mock objects.
You mentioned Spring as Inversion Of Control container. What is it's functionality?
In Spring, the objects are the backbone of the application. These objects managed by the Spring IOC are called beans. Spring IOC is responsible for instantiating, configuring and assembling of the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that compose your application and the rich inter-dependencies between such objects. 
 
How is the configuration metadata provided to Spring IOC Container or how is the Spring IOC container instantiated?
  • Through XML files
  • Through annotations ( starting Spring 2.5 )
  • Java-based Configuration ( starting Spring 3.0 )
It's very important that you understand how Spring IOC functions. Following will help you understand the inner working of the Spring IOC.
 
  • The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework's IoC container.
  • The org.springframework.beans.factory.BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object.
  • The BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory
The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that compose your application and the rich interdependencies between such objects.
 
Several implementations of the ApplicationContext interface are supplied out-of-the-box with Spring. In standalone applications it is common to create an instance ofClassPathXmlApplicationContext or FileSystemXmlApplicationContext.
 
 
What are the different ways for DI in Spring?
  • Constructor-based dependency injection
  • Setter-based dependency injection
  • Instantiation using an instance factory method
 
When does spring create the bean ?
  The Spring container validates the configuration of each bean as the container is created, including the validation of whether bean reference properties refer to valid beans. However, the bean properties themselves are not set until the bean is actually created. Beans that are singleton-scoped and set to be pre-instantiated (the default) are created when the container is created.Otherwise the bean and it's dependencies are created when the bean is requested.
  
What are different Bean scopes?
 
 Spring supports 5 different scopes, three are available for only web-aware ApplicationContext.
 
 
 singleton The scope of the Spring singleton is per container and per bean. It's different from GoF singleton as it is described as one and only oneinstance of a particular class per ClassLoader.
 prototype creates a new bean instance every time a request for that specific bean is made.
 request A new instance is created for every request.
 session A new instance is created for the lifetime of the session.
 global session Applicable to portlet-based applications. Allows sharing among all portlets. 
 
 
 
References :
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/htmlsingle/
Web Analytics