Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
What is loose coupling in spring with example
Instance are assigned to the interface reference so that Instance class name can be changed any time without touching all places.

class StudentHolder {
    StudentImpl sImpl;
    public StudentHolder(StudentImpl sImpl){
          this.sImpl = sImpl;

In Main class
StudentImpl sImpl = new Student();
StudentHolder sHolder = new StudentHolder(sImpl);

Student is a class, StudentImpl is a interface of Student. Here Student class name can change any time without touching StudentHolder class. this known as loose coupling objects. Student Object is loosly coupled with StudentHolder by StudentImpl interface.
Download as Zip 
Link to download
Spring Loose Coupling Example

Click on File menu. then click "Download"
Step.1 Start a Java Project with Spring Jars 
  1. Open Eclipse
  2. Click on menu New -> Others
  3. In wizards type "Java Project" and Select "Java Project"
  4. Click Next
  5. Enter project name as "SpringLooseCouplingExample", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
  7. Click Finish.
Step.2 Project Explorer Preview
package com.springexamples;

public interface StudentImpl {
    public int getStudentNo();
    public String getStudentName();
package com.springexamples;

public class Student implements StudentImpl {
    private int studentNo;
    private String studentName;
    public void setStudentNo(int studentNo) {
        this.studentNo = studentNo;
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    public int getStudentNo() {
        return studentNo;
    public String getStudentName() {
        return studentName;
package com.springexamples;

public class StudentHolder {
    StudentImpl sImpl;
    public StudentHolder(StudentImpl sImpl){
        this.sImpl = sImpl;
    public void display(){
        System.out.println("Student Details\n");
        System.out.println("No: "+this.sImpl.getStudentNo());
        System.out.println("Name: "+this.sImpl.getStudentName());
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean id="student" class="com.springexamples.Student">
        <property name="studentNo" value="101" />
        <property name="studentName" value="John Peter" />
    <bean id="sHolder" class="com.springexamples.StudentHolder">
        <constructor-arg ref="student" />

package com.springexamples;

import org.springframework.context.ApplicationContext;

public class RunMyProgram {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        StudentHolder holder = (StudentHolder) context.getBean("sHolder");
Final Step and Output 
Run "" as Java Application

Student Details

No: 101
Name: John Peter
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details