Spring Framework Introduction

Spring is a light weight and open source framework created by Rod Johnson in 2003.

Spring Framework made J2EE application development little easier, by introducing POJO model

Advantage of Spring


  • Spring is combination of technologies
  • The main feature of Spring is dependencies injection which helps you keep interface and implementation separate.
  • Aspect oriented programming, which isolates cross cutting concerns into modules that you can apply declaratively.
  • libraries to help with common tasks like persistence (JDBC, Hibernate, iBatis, JDO, JPA, etc.), remoting (RMI, HTTP, web services),asynch messaging (message driven POJOs), validating and binding, web MVC (Spring or Struts), utilities like e-mail, scheduling, security, etc
  • Spring’s application context is really just a big Factory Pattern.

Spring became more popular because of
— Loose Coupling
— Simplicity
— Testability

Dependency Injection


We use Dependency Injection (DI) to implement loose coupling.
The choice of any particulary DI Container is not that important.

Every time you create an instance of a class by using the new keyword,
you tightly couple your code to that class
,
and you will not be able to substitute that particularl implementation
with a different one (at least not without recompiling the code).

Let’s Understand it With Example.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
package com.spy;
 
public class Company 
{
 
	Employee emp;
 
	public Company() 
	{	
		emp=new Employee();
	}
 
}

In Above code Company and Employee object is tightly coupled with each other.


There is a Design Pattern which Helps you to make your code loosely coupled. i.e Factory Design Pattern.

1
2
3
4
5
6
7
8
9
10
11
12
 
public class Company 
{
 
	Employee emp;
 
	public Company(Employee emp) 
	{	
		this.emp=emp;
	}
 
}

Now Employee and Company object is loosely coupled with Each other.
Again some one has to pass Employee object.
So Spring inject this dependencies based on xml configuration.we will
understand it with Example.

Reference link:

Loose coupling

Setting up development Environment for spring framework in eclipse

In order to Create Spring project Spring Jar file is required.

• org.springframework.beans
• org.springframework.core
• org.springframework.context

Click Here to Download Spring jar

Based on project type and functionality we need to add other spring jar file.
For each project you have to add jar file.

So first we will create User library and we will add that Library in each project.

Steps to create User Library



Right click on project and select build path and click on add Libraries



Select User library and click on next


Click on User Libraries



Click on new to Create Library


Give Library name



Select Spring-lib and click on add External jar



Browse and Select all Required jar file



Click on Ok



Click on Finish


How to add Jar file ion Other Project



Step 1: Right click on project and select build path and click on add Libraries


Step 2: Select user library


Step 3: Select Spring-lib library which you have created


Step 4: Here you can see that Library successfully added into project.


Creating First Spring project

This project is developed using Eclipse IDE.

You need to add Spring jar file:
Click Here to Download Spring jar

Employee.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package org.training;
 
public class Employee 
{
 
	public void Test()
	{
 
		System.out.println("Test Method is called");
 
	}
 
}

TestMain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.training;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestMain 
{
	public static void main(String[] args)
	{
		System.out.println("**********************");
		ApplicationContext context=new ClassPathXmlApplicationContext("Spring-config.xml");
		Employee e=	(Employee)context.getBean("emp");
	        e.Test();
	        System.out.println("***********************");
	}
}

Spring-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
 
<bean id="emp" class="org.training.Employee"/>  
 
 
</beans>

Output:
Project Structure

Here we Use Application Context object to get Object of Employee.
You can also use Bean factory to do the same but Application Context is more efficient way to do it.
TestMain2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.training;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
 
public class TestMain2 
{
 
 
	public static void main(String[] args)
	{
 
		System.out.println("**********************");
 
		BeanFactory factory=new XmlBeanFactory(new ClassPathResource("Spring-config.xml"));
 
		Employee e=(Employee) factory.getBean("emp");
 
	        e.Test();
 
	        System.out.println("***********************");
 
	}
}

Output: