当前位置 : 主页 > 手机开发 > 其它 >

IOC的三种DI依赖注入(构造注入,属性注入,注解注入)

来源:互联网 收集:自由互联 发布时间:2021-06-22
?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.springframe
<?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-4.3.xsd">
    <!-- 第一种无参构造注入 -->
    <bean class="com.bb.bean.Student" id="student" />

    <!-- 第二种有参构造  -->
    <!-- 通过name-->
    <bean class="com.bb.bean.Student" id="student1">
        <constructor-arg name="name" value="王五" />
        <constructor-arg name="age" value="13" />
        <constructor-arg name="stuID" value="A1" />
    </bean>

    <!-- 通过参数下标 -->
    <bean class="com.bb.bean.Student" id="student2">
        <constructor-arg index="0" value="狄仁杰" />
        <constructor-arg index="1" value="30" />
        <constructor-arg index="2" value="E1" />
    </bean>



    <!-- P名称空间注入-->
    <bean class="com.bb.bean.Student" id="stuP" p:name="赵六" p:age="13"
        p:stuID="B1" />

    <!-- 第三种属性注入 -->
    <bean class="com.bb.bean.Student" id="student3">
        <property name="name" value="李元芳" />
        <property name="age" value="16" />
        <property name="stuID" value="c1" />
    </bean> 
    
    <!-- 对象注入,属性是对象的注入 -->
     <bean class="com.bb.bean.Teachar" id="teachar">
        <property name="name" value="德莱文" />
        <property name="className" value="D1" />
    </bean>
    
    <bean class="com.bb.bean.Student" id="student4">
        <property name="name" value="卡特" />
        <property name="age" value="16" />
        <property name="stuID" value="F1" />
        <property name="teachar" ref="teachar"/>
    </bean> 
    
    <!-- 属性是集合的注入 list set map Properties -->
    <bean class="com.bb.bean.Teachar" id="teachar">
        <property name="name" value="德莱文" />
        <property name="className" value="D1" />
    </bean>
    <bean class="com.bb.bean.Teachar" id="teachar1">
        <property name="name" value="德莱の斯" />
        <property name="className" value="D2" />
    </bean>
    
    <bean class="com.bb.bean.Student" id="student5">
        <property name="name" value="卡特" />
        <property name="age" value="16" />
        <property name="stuID" value="F1" />
        
        <property name="teaList">
            <list>
                <ref bean="teachar" />
                <ref bean="teachar1" />
            </list>
        </property>
        
        <property name="teaSet">
            <set>
                <ref bean="teachar"/>
                <ref bean="teachar1"/>
            </set>
        </property>
        
        <property name="teaMap">
            <map>
                <entry key="t1" value-ref="teachar"/>
                <entry key="t2" value-ref="teachar1"/>
            </map>
        </property>
        
        <property name="pro">
            <props>
                <prop key="p1">盖伦</prop>
                <prop key="p2">亚索</prop>
            </props>
        </property>
    </bean>




</beans>
网友评论