I want to write my own simple DI framework. I want that it perform only this simple case like Spring does:
public interface IWriter {
public void writer(String s);
}
@Service
public class MySpringBeanWithDependency {
private IWriter writer;
@Autowired
public void setWriter(IWriter writer) {
this.writer = writer;
}
public void run() {
String s = "This is my test";
writer.writer(s);
}
}
@Service
public class NiceWriter implements IWriter {
public void writer(String s) {
System.out.println("The string is " + s);
}
}
public class Main extends TestCase {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"META-INF/beans.xml");
MySpringBeanWithDependency test = (MySpringBeanWithDependency) context
.getBean("mySpringBeanWithDependency");
test.run();
}
}
The same case, but mb without an XML-file.
Can somebody explain the concept of this type of frameworks and write some code.