自动装配的优势是什么?自动装配在春天的优势是什么
春季自动装配的一个例子是像
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();
}
}
的test.xml
<bean id="test" class="TestClass">
同样在正常操作中可以用做:
public class MainClass {
public static void main(String[] args) {
TestClass obj = new TestClass();
obj.testMethod();
}
}
什么是春天的优势,我的意思是我听说过术语控制和依赖注入的反转。 在这两个示例中,TestClass的引用都是通过new
操作员再次通过Spring XML使用的。那么,简单地说,有人可以解释什么是优势。
如果你的应用程序由一个单独的类组成,那么依赖注入是没有用的,因为你有0个依赖注入。当组件取决于其他组件时,它变得很有用。就像典型的Web应用程序一样,UI消费者依赖于依赖于其他服务和DAO的业务服务。 – 2013-05-02 20:04:37