| Free tutorials for Java, Eclipse and Web programming |
We will later use the following datamodel for the example.
Create a Java project "de.vogella.spring.di.model" and create the following packages and classes.
package writer;
public interface IWriter {
public void writer(String s);
}
package writer;
public class Writer implements IWriter {
public void writer (String s){
System.out.println(s);
}
}
package writer;
public class NiceWriter implements IWriter {
public void writer (String s){
System.out.println("The string is " + s);
}
}
package testbean;
import writer.IWriter;
public class MySpringBeanWithDependency {
private IWriter writer;
public void setWriter(IWriter writer) {
this.writer = writer;
}
public void run() {
String s = "This is my test";
writer.writer(s);
}
}
The class "MySpringBeanWithDependency.java" contains a setter for the actual writer. We will use the Spring Framework to inject the correct writer into this class.