自动装配的优势到底是什么?
春天自动装配的一个例子就像
public class TestClass {
testMethod() {
// .....
};
}
public class MainClass {
public static void main(String[] args) {
ApplicationContext ctx = new ClasspathXmlApplicationContext("test.xml");
TestMethod obj = (TestClass) ctx.getBean("test");
obj.testMethod();
}
}
测试.xml
<bean id="test" class="TestClass">
可以使用以下方法完成正常操作中的相同操作:
public class MainClass {
public static void main(String[] args) {
TestClass obj = new TestClass();
obj.testMethod();
}
}
Spring 的优势是什么,我的意思是我听说过控制反转和依赖注入这两个术语。在这两个示例中,TestClass 的引用通过 Spring XML 再次通过new
操作器使用。那么有人可以简单地解释一下优势是什么。