javascript
Spring基于Setter函数的依赖注入(DI)
以下內容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-setter-based-dependency-injection.html:
當容器調用一個無參的構造函數或一個無參的靜態factory方法來初始化你的bean后,通過容器在你的bean上調用Setter函數,基于Setter函數的DI就完成了。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jsoft.testspring</groupId><artifactId>testbeansetter</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>testbeansetter</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- Spring Core --><!-- http://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.4.RELEASE</version></dependency><!-- Spring Context --><!-- http://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.4.RELEASE</version></dependency></dependencies> </project>SpellChecker.java:
package com.jsoft.testspring.testbeansetter;public class SpellChecker {public SpellChecker(){System.out.println("SpellChecker無參數構造函數初始化");}public void checkSpelling(){System.out.println("SpellChecker檢查方法");} }TextEditor.java:
package com.jsoft.testspring.testbeansetter;public class TextEditor {private SpellChecker spellChecker;public void setSpellChecker(SpellChecker spellChecker){System.out.println("TextEditor通過setter初始化");this.spellChecker = spellChecker;}public void spellCheck() {this.spellChecker.checkSpelling();}}beans.xml:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="spellChecker" class="com.jsoft.testspring.testbeansetter.SpellChecker"></bean><bean id="textEditor" class="com.jsoft.testspring.testbeansetter.TextEditor"><property name="SpellChecker" ref="spellChecker"></property></bean></beans>唯一的區別就是在基于構造函數注入中,我們使用的是<constructor-arg>標簽,而在基于Setter函數的注入中,我們使用的是<property>標簽。
第二個你需要注意的點是,如果你要把一個引用傳遞給一個對象,那么你需要使用ref屬性,而如果你要直接傳遞一個值,那么你應該使用value屬性。
App.java:
package com.jsoft.testspring.testbeansetter;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Hello world!**/ public class App {public static void main( String[] args ){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");textEditor.spellCheck();} }運行結果:
下面將介紹使用p-namespace實現XML配置:
如果你有許多的Setter函數方法,那么在XML配置文件中使用p-namespace是非常方便的。區別如下:
以下為使用<property>標簽的配置:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="john-classic" class="com.example.Person"><property name="name" value="John Doe"/><property name="spouse" ref="jane"/></bean><bean name="jane" class="com.example.Person"><property name="name" value="John Doe"/></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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="john-classic" class="com.example.Person"p:name="John Doe"p:spouse-ref="jane"></bean><bean name="jane" class="com.example.Person"p:name="John Doe"></bean></beans>看起來非常的整潔,在這里,您應該注意到使用p-namespace指定原始值和對象引用的區別。-ref部分表示這不是一個直接的值,而是引用另一個bean。
?
測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test8/testbeansetter
轉載于:https://www.cnblogs.com/EasonJim/p/6883587.html
總結
以上是生活随笔為你收集整理的Spring基于Setter函数的依赖注入(DI)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript语言中文参考手册.c
- 下一篇: 第二阶段冲刺 第六天