依賴注入
當編寫一個複雜 的java應用程序時,應用程序類應該儘可能獨立於其他類來增加這些類重用的可能性。依賴注入可以理解爲有助於將各種類並在一起,同時保持各種類的獨立。
如:private Dog dog;//將其他類作爲本類的屬性
public class Demo{
private Dog dog;
public Demo(Dog dog){ //構造函數注入
this.dog=dog;
}
public void setDog(Dog dog){ //setter注入
this.dog=dog;
}
}
依賴注入有兩種
序號 | 依賴注入類型 & 描述 |
---|---|
1 | Constructor-based dependency injection
當容器調用帶有多個參數的構造函數類時,實現基於構造函數的 DI,每個代表在其他類中的一個依賴關係。 |
2 | Setter-based dependency injection
基於 setter 方法的 DI 是通過在調用無參數的構造函數或無參數的靜態工廠方法實例化 bean 之後容器調用 beans 的 setter 方法來實現的。 |
Spring基於構造函數的依賴注入
package dc.com;
public class Cat {
private Dog dog;
public Cat(Dog dog){
System.out.println("this is CatClass....");
this.dog=dog;
}
public void CatMe(){
dog.Dogme();
}
}
package dc.com;
public class Dog {
public Dog(){
System.out.println("this is DogClass.....");
}
public void Dogme(){
System.out.println("this is Dogme......");
}
}
package dc.com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
Cat cat= (Cat) applicationContext.getBean("cat");
cat.CatMe();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 由 Spring容器創建該類的實例對象 -->
<bean id="cat" class="dc.com.Cat" >
<constructor-arg ref="dog"></constructor-arg>
</bean>
<bean id="dog" class="dc.com.Dog"/>
</beans>
Spring基於setter方法的依賴注入
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
可以使用 p-namespace方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor"
p:spouse-ref="spellchecker">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>