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


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

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


Note
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

Hints.
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 
StudentImpl.java
package com.springexamples;

public interface StudentImpl {
    public int getStudentNo();
    public String getStudentName();
}
Student.java
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;
    }
}
StudentHolder.java
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());
    }
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

</beans>
RunMyProgram.java
package com.springexamples;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

Output
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
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details