2
的我想写我自己的简单的DI框架。 我希望它像春天确实只执行这个简单的例子:基础依赖注入框架
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();
}
}
同样的情况,但MB没有一个XML文件。
有人能解释这种框架的概念,并编写一些代码。
为什么要重新发明轮子? –
我不想重新发明?我想知道它是如何工作的。 – itun
阅读Spring文档。然后阅读源代码。 –