美文网首页
Spring--依赖注入之为.*?类型属性赋值

Spring--依赖注入之为.*?类型属性赋值

作者: 何以解君愁 | 来源:发表于2022-08-11 14:36 被阅读0次
为类类型属性赋值: 
Student构造方法变量: 
    private String name;
    private Integer sid;
    private Classes classes;
.xml(rel:引用  IOC容器某个bean的id)(外部bean):
    <bean id="Student1" class="com.Student">
        <property name="name">
            <value><![CDATA[jack]]></value>
        </property>
        <property name="sid" value="99"></property>
        <property name="classes" ref="classes"></property>
    </bean>

    <bean id="classes" class="com.Classes">
        <property name="cid" value="1"></property>
        <property name="cname" value="classOne"></property>
    </bean>
(内部bean,只能在bean的内部使用,不能直接通过IOC容器获取):
    <bean id="Student1" class="com.Student">
        <property name="name">
            <value><![CDATA[jack]]></value>
        </property>
<!--        <property name="sid" value="99"></property>-->
<!--        <property name="classes" ref="classes"></property>-->
        <property name="classes">
            <bean id="classInner" class="com.Classes">
                <property name="cid" value="1"></property>
                <property name="cname" value="classOne"></property>
            </bean>
        </property>
    </bean>
为数组属性赋值,直接在bean中添加:
        <property name="hobby">
            <array>
                <value>厂公</value>
                <value>锦衣卫指挥使</value>
            </array>
        </property>
为类类型数组赋值将value换成ref
为list集合赋值,两种方式(构造函数private List<Student> Students;):
① <bean id="classes1" class="com.Classes">
        <property name="cname" value="7"></property>
        <property name="cid" value="5"></property>
        <property name="students">
            <list>
    //可以用value
                <ref bean="Student1"></ref>
                <ref bean="Student2"></ref>
            </list>
        </property>
    </bean>
② <bean id="classes1" class="com.Classes">
        <property name="cname" value="7"></property>
        <property name="cid" value="5"></property>
        <property name="students" ref="StudentList"></property>
<!--        <property name="students">-->
<!--            <list>-->
<!--                <ref bean="Student1"></ref>-->
<!--                <ref bean="Student2"></ref>-->
<!--            </list>-->
<!--        </property>-->
    </bean>

    <!--配置一个集合类型的bean需配置util约束-->
    <util:list id="StudentList">
        <ref bean="Student1"></ref>
        <ref bean="Student2"></ref>
    </util:list>
为map集合属性赋值,两种方式(构造函数private Map<String,Teacher> teachers;):
① <bean id="Student1" class="com.Student">
        <property name="name">
            <value><![CDATA[jack]]></value>
        </property>
<!--        <property name="sid" value="99"></property>-->
<!--        <property name="classes" ref="classes"></property>-->
        <property name="classes">
            <bean id="classInner" class="com.Classes">
                <property name="cid" value="1"></property>
                <property name="cname" value="classOne"></property>
            </bean>
        </property>

        <property name="hobby">
            <array>
                <value>厂公</value>
                <value>锦衣卫指挥使</value>
            </array>
        </property>
        <property name="teachers">
            <map>
                <entry key="134" value-ref="teacher"></entry>
            </map>
        </property>
    </bean>

    <bean id="teacher" class="com.Teacher">
        <property name="cid" value="134"></property>
        <property name="cname" value="风男"></property>
    </bean>
②与List几乎相同

相关文章

网友评论

      本文标题:Spring--依赖注入之为.*?类型属性赋值

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