自動裝配的優勢是什麼?自動裝配在春天的優勢是什麼
春季自動裝配的一個例子是像
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