美文网首页
依赖注入xml配置形式

依赖注入xml配置形式

作者: 充满智慧的白痴 | 来源:发表于2019-12-30 10:29 被阅读0次

    基于构造函数

    当容器调用带有一组参数的类构造函数时,基于构造函数的 DI 就完成了,其中每个参数代表一个对其他类的依赖。即向bean的构造函数传参

     <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <!-- constructor-arg标签将向bean的构造函数传递参数 -->
         <!-- index指定位置,可无-->
          <!--type指定基础类型,value指定值 -->
          <constructor-arg ref="args1" index="0" />
          <constructor-arg type="String"  index="0" value="zs"/> 
       </bean>
    <bean id="args1" class="com.tutorialspoint.Args">
    </bean>
    

    基于设置值

    <!-- 使用property标签 --> 
    <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <property name="spellChecker" ref="spellChecker"/>
    </bean>
    

    内部bean

    即某个bean确定只在另外一个bean的内部使用

    <bean id="Student" class="com.tutorialspoint.Student">
          <property name="school">
             <bean id="school" class="com.tutorialspoint.School"/>
           </property>
     </bean>
    

    特殊类型 list map

    <property name="addressList">
             <list>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
             </list>
          </property>
    
          <!-- results in a setAddressSet(java.util.Set) call -->
          <property name="addressSet">
             <set>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
            </set>
          </property>
    
          <!-- results in a setAddressMap(java.util.Map) call -->
          <property name="addressMap">
             <map>
                <entry key="1" value="INDIA"/>
                <entry key="2" value="Pakistan"/>
                <entry key="3" value="USA"/>
                <entry key="4" value="USA"/>
             </map>
          </property>
    
          <!-- results in a setAddressProp(java.util.Properties) call -->
          <property name="addressProp">
             <props>
                <prop key="one">INDIA</prop>
                <prop key="two">Pakistan</prop>
                <prop key="three">USA</prop>
                <prop key="four">USA</prop>
             </props>
          </property>
    

    自动装配

    属性的name对应其他bean的id,或者属性的type对应其他bean的id

    // 如果TextEditor中有一个属性的name是spellChecker,则将spellChecker注入到textEditor中
    <bean id="textEditor" class="com.tutorialspoint.TextEditor"  autowire="byName">
          <property name="name" value="Generic Text Editor" />
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    // 如果TextEditor中有一个属性的类型是SpellChecker,则将SpellChecker注入到textEditor中
    <bean id="textEditor" class="com.tutorialspoint.TextEditor"  autowire="byType">
          <property name="name" value="Generic Text Editor" />
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    // 如果该bean的构造函数的某个参数的类型等于另外一个bean的id,则将那个bean注入到构造函数的参数中
      <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
          autowire="constructor">
          <constructor-arg value="Generic Text Editor"/>
       </bean>
     <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    

    相关文章

      网友评论

          本文标题:依赖注入xml配置形式

          本文链接:https://www.haomeiwen.com/subject/efefpctx.html