×
☰ See All Chapters

Spring Tutorial

Our spring tutorial brings you the complete in depth knowledge on spring. We have provided crispy and clear details on every terms and concepts of spring. This spring tutorial is designed and organized to bring you a strong foundation on which you can take yourself to higher levels of expertise.

Spring is an open source framework, provides abstraction layer over JSE, JEE module technologies like servlet, JSP, JDBC, JNDI, JMS, Java Mail etc.  Framework is special software that is developed based on core technologies having capability to develop the common logics of the application development dynamically. This process makes the application developer to just concentrate on application specific logics of the development. This increases the productivity in application development. Doing more work in less time with good accuracy is called productivity.

Vendor of Spring: Interface 21

Creator of Spring: Mr. Rod Jhonson

Spring can develop standalone, desktop, two-tier, web, distributed applications. Hence it is called horizontal technology. Where Hibernate, JPA are vertical technology because it can be used for writing only persistence logic.

Spring is an open source, lightweight, loosely coupled, aspect-oriented, container, dependency injection based Java-JEE framework to develop all kinds of applications in java.

Lightweight

  • Spring is lightweight in terms of both size and overhead because, 

  • To run Spring the heavy weight app server(EJB container) and web server(servlet/JSP container) are not required (for most of the Spring Applications). 

  • Spring provides its own light weight containers as BeanFactory containers or ApplicationContext containers which can be activated anywhere and at any time just by creating objects for some predefined classes. 

  • Spring Framework comes in a single JAR file and is just 2.5 MB. (spring 2.5 version) 

  • Spring is noninvasive because, it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API. 

The servlet, JSP, EJB etc… components of JEE are heavy weight containers/servers for execution and the resources of these components are their respective API dependent.

Loosely coupled

If degree of dependency is very less between two components then they are called loosely coupled components.

If degree of dependency is very high between two components then they are called tightly coupled components.

  • CPU and Monitor are tightly coupled devices. 

  • TV and remote are loosely coupled devices. 

Spring is loosely coupled because,

  • We can use specific spring module/modules or all spring modules in app development( The degree of dependency is less between spring modules) 

  • We can use only spring to develop whole project or we can integrate spring with other technologies to develop the project. 

Aspect Oriented (AOP)

Aspect means middleware services like security, transaction management etc.

The AOP module of spring provides facilities for the programmer to work with built in middleware services and third party middleware services. This module also allows the programmer to develop user defined middleware services.

These features of spring make the spring framework as aspect oriented.

By this type of programming we can separate or isolate application business logic from commonly required middle level services (cross cutting concerns) like security, transaction, logging, etc. The commonly required services code can be centralized / modularized and can be reused at various places. The main advantage of this is loose coupling and reduces maintenance.

What are middleware/middle level services ?

Middleware services are additional, optional services/logics which can be applied on the applictaion to run the application perfectly and accurately in all the situations. (16+ middleware servces exists)

Example:

  • Security services (protect the application). 

  • Transaction Management services( executes the app logics by applying do everything or nothing principle eg.transfer money task logic) 

  • Logging services(keeps track of app flow of execution through log messages or confirmation messages) 

  • Connection Pooling services 

Container

Spring is responsible to create instances, invoke life cycle methods, manage the lifecycle of the objects and also provide the dependency injection i.e. associate the dependencies between the beans.

Container is a software that takes care of the whole life cycle of given resources or component from birth to death.

Example:  

Applet viewer (Container takes care of applet life cycle)

Servlet container (Container takes care of servlet life cycle)

Spring container (Container takes care of Spring resources(like classes) life cycle)

In spring we have two built in containers, they are

  1.  
  2.  

Spring containers as given as predefined java classes, by just creating objects for these classes we can activate spring container anywhere. This indicates spring containers are light weight containers.

Spring containers are not alternate containers for servlet, JSP, EJB. Resource means a program or file of an application. In spring, Resource means the classes and interfaces of spring application.

Dependency Injection or Inversion of Control (DI or IOC)

DI means, instead of an object looking up dependencies from a container, the container gives the dependencies to the object at instantiation. If you want to call any business functionality you need to create instance for the required component and sometimes you need to look up registry for some other component. To eliminate this spring offers Inversion of Control (IOC) where spring container is responsible to create and inject the required instances. Spring container is responsible to create the bean instances and also responsible to associate the dependencies. The main advantage of DI is loose coupling.

If resources/application is spending time to search and gather its dependent values then it is called dependency lookup.

In dependency lookup resources/application pulls the values from other resources.

Ex: Student gathers his course materials (dependent value) from institute by requesting for it. (Gathering explicitly)

If underlying container or framework or server is dynamically assigning values to resources/applications then it is called as dependency injection/Inversion of control.

In dependency injection underlying container or framework or server dynamically pushes dependent values to resources/applications.

Ex: If student is getting course materials automatically the moment he registered for course.

In spring, values required for a spring resources/applications will be assigned by spring framework or spring containers dynamically.

You might hear the terms Inversion of Control and Dependency Injection used interchangeably, but in fact they are not the same thing. Inversion of Control is a much more general concept, and it can be expressed in many different ways. Dependency Injection is merely one concrete example of Inversion of Control.

Spring Modules

The Spring Framework contains a lot of features, which are well-organized in about twenty modules. These modules can be grouped together based on their primary features into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation and Test. These groups are shown in the diagram below.

                             spring-tutorial-0
 

We will study these modules step by step in coming chapters.


All Chapters
Author