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
-
Open Eclipse
-
Click on menu New -> Others
-
In wizards type "Java Project" and Select "Java Project"
-
Click Next
-
Enter project name as "SpringLooseCouplingExample", then click Next
-
Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
-
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="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>
|
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
|