[关闭]
@yudesong 2018-02-16T13:19:38.000000Z 字数 2553 阅读 499

Spring DI依赖注入

依赖注入


序号 依赖注入类型 & 描述
Constructor-based dependency injection 当容器调用带有多个参数的构造函数类时,实现基于构造函数的 DI,每个代表在其他类中的一个依赖关系
Setter-based dependency injection 基于 setter 方法的 DI 是通过在调用无参数的构造函数或无参数的静态工厂方法实例化 bean 之后容器调用 beans 的 setter 方法来实现的

代码实例

  1. public class User {
  2. private String name;
  3. public String getName() {
  4. return name;
  5. }
  6. public void setName(String name) {
  7. this.name = name;
  8. }
  9. }

Beans.xml

  1. <bean id="user" class="com.bean.User">
  2. <property name="name">
  3. <value>yudesong</value>
  4. </property>
  5. </bean>
  6. <bean id="user" class="com.bean.User">
  7. <constructor-arg value="name" index="0">
  8. </constructor-arg>
  9. </bean>

注入内部 Beans

TextEditor.java

  1. public class TextEditor {
  2. private SpellChecker spellChecker;
  3. // a setter method to inject the dependency.
  4. public void setSpellChecker(SpellChecker spellChecker) {
  5. System.out.println("Inside setSpellChecker." );
  6. this.spellChecker = spellChecker;
  7. }
  8. // a getter method to return spellChecker
  9. public SpellChecker getSpellChecker() {
  10. return spellChecker;
  11. }
  12. public void spellCheck() {
  13. spellChecker.checkSpelling();
  14. }
  15. }

SpellChecker.java

  1. public class SpellChecker {
  2. public SpellChecker(){
  3. System.out.println("Inside SpellChecker constructor." );
  4. }
  5. public void checkSpelling(){
  6. System.out.println("Inside checkSpelling." );
  7. }
  8. }

Beans.xml

  1. <bean id="textEditor" class="com.tutorialspoint.TextEditor">
  2. <property name="spellChecker">
  3. <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
  4. </property>
  5. </bean>

注入集合

Spring 提供了四种类型的集合的配置元素

元素 描述
<list> 它有助于连线,如注入一列值,允许重复
<set> 它有助于连线一组值,但不能重复
<map> 它可以用来注入名称-值对的集合,其中名称和值可以是任何类型
<props> 它可以用来注入名称-值对的集合,其中名称和值都是字符串类型
  1. List addressList;
  2. Set addressSet;
  3. Map addressMap;
  4. Properties addressProp;
  5. <!-- Definition for javaCollection -->
  6. <bean id="javaCollection" class="com.tutorialspoint.JavaCollection">
  7. <!-- results in a setAddressList(java.util.List) call -->
  8. <property name="addressList">
  9. <list>
  10. <value>INDIA</value>
  11. <value>Pakistan</value>
  12. <value>USA</value>
  13. <value>USA</value>
  14. </list>
  15. </property>
  16. <!-- results in a setAddressSet(java.util.Set) call -->
  17. <property name="addressSet">
  18. <set>
  19. <value>INDIA</value>
  20. <value>Pakistan</value>
  21. <value>USA</value>
  22. <value>USA</value>
  23. </set>
  24. </property>
  25. <!-- results in a setAddressMap(java.util.Map) call -->
  26. <property name="addressMap">
  27. <map>
  28. <entry key="1" value="INDIA"/>
  29. <entry key="2" value="Pakistan"/>
  30. <entry key="3" value="USA"/>
  31. <entry key="4" value="USA"/>
  32. </map>
  33. </property>
  34. <!-- results in a setAddressProp(java.util.Properties) call -->
  35. <property name="addressProp">
  36. <props>
  37. <prop key="one">INDIA</prop>
  38. <prop key="two">Pakistan</prop>
  39. <prop key="three">USA</prop>
  40. <prop key="four">USA</prop>
  41. </props>
  42. </property>
  43. //处理NULL
  44. <bean id="..." class="exampleBean">
  45. <property name="email"><null/></property>
  46. </bean>
  47. //处理空字符串
  48. <bean id="..." class="exampleBean">
  49. <property name="email" value=""/>
  50. </bean>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注