Introduction to Spring Framework, IoC and Injection
Introduction to Spring Framework
Spring Framework is one of the most widely used application frameworks for Java Enterprise applications. Spring is an open source framework created to address the complexities in developing Java enterprise applications using EJB standards. The framework was initially written by Rod Johnson, who released it with his publication of his book Expert One-on-One J2EE Development without EJB in October 2002.
Spring’s main aim was to make Java EE development easier. Let’s touch base on a few of the issues with J2EE, especially at the time of introduction.
- J2EE required excessive amount support or plumbing code, detracting from development effort on concentrating on writing domain or application specific code.
- Many EJB applications use a distributed object model when it was not appropriate
- EJB component model was unnecessarily complex
- EJB was overused, while some require components were underused
Spring Lightweight Framework
Spring is considered a “lightweight” framework both because of its memory footprint and because of the services it provides in comparison to J2EE container. As mentioned earlier, EJB 2.1 and earlier required extensive configuration and additional plumbing, Spring eliminates many of these shortcomings. A lightweight framework aims to reduce complexity in both application code and avoid unnecessary complexity in their own functioning. In Spring you can pick and choose which modules/components to use depending on your application needs. In other words, if my application requires JDBC and JMS we can load those modules and exclude say Portlet, Remoting and Struts modules.
Yet a framework should provide guidance with respect to good practice. It should make the right thing easy to do. Getting the right mixture of constraint and freedom is the key challenge of framework design, which is as much art as science.
–Rod Johnson
Spring Framework Modules
The Spring Framework consists of 20 modules organized into logical groupings. These modules include core, beans, context, expression language, Messaging, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts, and Portlet. These modules are further grouped into Spring Core Container, Test, AOP, Aspects, Instrumentation, Data Access & Integration and Web. The figure below best explains how Spring organizes these modules.

Image Courtesy: SpringSource
Benefits of Spring Framework
- Spring is non-invasive – it aims to minimize the impact of application code in the framework.
- Spring promotes choice – Spring allows us to interchange one framework to another without impacting our business logic code. We can switch from one framework to another from say Struts to Spring MVC.
- Spring does not reinvent the wheel – It makes use of popular frameworks and technologies like ORM frameworks, logging frameworks, etc.
- Spring facilitates Object Oriented design in Java EE applications – Spring allows one to build code that is better factored, is more coherent, is reusable and is loosely coupled.
- Spring provides a consistent programming model – Spring provides a model that separates the application code from the environment details such as Database, making code cleaner in its runtime context.
- Spring is designed with testability in mind – Spring strives to use POJOs for application objects. POJOs are easy to test to stub and mock. By using POJOs, it becomes easy to set up dependency injection in the form of push configurations and test data into these application object POJOs at runtime.
Dependency Injection (DI)
If there is one thing that Spring is most identified by, it has to be its Dependency Injection flavor of Inversion of Control. It is at the heart of the Spring framework. Dependency injection has the concept of a Factory that instantiates the objects and “injects” them into other dependent objects.
Spring Supports several types of Dependency Injection:
- Setter Injection – The most commonly used form today. The injection of dependencies via setter methods defined in Spring configuration file.
- Constructor Injection – The injection of dependencies via constructor arguments injected during instance instantiation.
- Method Injection – A less widely used form of dependency injection in which the container is responsible for implementing methods at runtime.
Inversion of Control (IoC)
In a typical application, we generally brak up the code into logical components or services that somehow interact with each other. In Java, these components are usually instances of Java classes or objects. These objects work together with other objects in order to complete their tasks. As an example, if Object A needs to work with Object B and Object C in order to complete its task, we say that Object A is dependent on Object B and C — in other words these two objects are its dependencies. Without Inversion of Control (IoC) we would need to wire these dependencies ourselves in the code where we need the dependency.
Inversion of Control (IoC) refers to the architectural pattern of having some outside entity (the Spring container) instantiate and wire objects together for us, such that objects are given the dependencies by the container instead of the developer having to do it through Java code. The container “injects” the dependencies instead of the developer hardwiring it in the code.
Let’s look at an example with this class BackupService, which is responsible for backing up files on to some file system in case of emergency. Obviously, we can have multiple BackupDevice implementations like disk, tape, flash, offsite (web), etc.
package com.avaldes.tutorial; public class BackupService { private BackupDevice device = new SanDiskSystem(); public BackupService() { } public boolean backupFile(File file) { system,out.println("Backing up file: " + file); boolean status = device.save(file); return status; } }
The way it is coded now, whoever uses the BackupService is forced into using the SanDiskSystem for backup purposes. We should be able to use other devices and/or mediums for backup purposes but doing so will require us to make additional changes and possibly write conditional logic in our code to work the other options into the mix.
We can modify the code, and add a setter method for device to make it more flexible.
package com.avaldes.tutorial; public class BackupService { private BackupDevice device; public BackupService() { } public setDevice(BackupDevice device) { this.device = device; } public boolean backupFile(File file) { system,out.println("Backing up file: " + file); boolean status = device.save(file); return status; } }
Now we can do the following in our main that looks like:
BackupService backup = new BackupService(); backup.setDevice(new FlashDrive()); backup.backupFile(fileXYZ);
Now our BackupService is not as limited as it once was to the SanDiskSystem we defined in the first example. However, as you see from the code above, we are still performing the wiring of the devices in the code. If we run this code in development, testing and production environments we may still need to make code changes as not all environments may be able to support said devices.
Let’s see how Spring can do this for us.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="flashdrive" class="com.avaldes.tutorial.FlashDiskSystem" /> <bean id="sandrive" class="com.avaldes.tutorial.SanDiskSystem" /> <bean id="tapedrive" class="com.avaldes.tutorial.TapeSystem" /> <bean id="backupService" class="com.avaldes.tutorial.BackupService"> <property name="device" ref="flashdrive" /> </bean> </beans>
Related Posts
- Introduction to Spring Framework, IoC and Injection
In this tutorial we will cover a basic introduction the Spring framework, Inversion of Control and Dependency Injection. We will discuss the benefits of using it, and why it is so popular in today's environment. - Spring Framework Constructor Injection Example
In this tutorial, we will concentrate on Constructor Injection. As the name suggests, constructor injection is form of dependency injection that is accomplished when we supply the arguments to the constructor so that when the bean is instantiated by the factory it contains all of the necessary information to be property constructed. - Spring Framework Constructor Injection with Collections Example
In this tutorial, we will modify our previous example and add a few collections so we can illustrate how we use constructor injection with differing collection types. Specifically, we will show how to perform constructor injection with List, Map and Set objects. - Spring Framework Setter Injection with Examples In this tutorial we will discuss Spring Framework Setter Injection which is the main method of dependency injection in Spring. The property element is used to define the setter injection in Spring by using the name of the property element and looking for the corresponding setXXXXXX methods in the specified bean.
- Spring Bean Scopes Example
In this tutorial we will begin covering scope of Spring beans. In Spring when you define a bean in the Spring configuration file, you are telling Spring about its scope whether you define it or not. This is a powerful and flexible approach because you can choose the scope of the objects you create via configuration instead of having to hardcode the scope of an object at the Java class level. - Spring Bean Life Cycle Example – @PostConstruct, @PreDestroy, InitializingBean, DisposableBean, init-method and destroy-method
In this tutorial we will cover the Spring Bean Life Cycle and the methods, attributes and annotations used to hook into the management of the bean life cycle in the IoC container. - Integrating Java JMX Managed Beans using Spring and Annotations
This post will show how to create managed beans using the JMX standard using Spring. JMX stands for Java Management Extensions, and was created to implement a standard and uniform way to manage your applications. By using JMX remote can connection to a JVM and monitor running applications on that JVM. In addition to monitoring, remote clients are able to directly run methods and modify runtime parameters of the application that is being managed. - Integrating Spring Message Driven POJO with ActiveMQ
This post will show how to create a message driven POJO that can send and receive JMS message using ActiveMQ as the Message Oriented Middleware (MOM). We will discuss various approaches to creating the MDPs (Message Driven POJOs) in this tutorial and discuss setting up ActiveMQ from Apache.
Please Share Us on Social Media






Leave a Reply