0
点赞
收藏
分享

微信扫一扫

xml注入list集合类型,值是对象

老牛走世界 2022-03-18 阅读 47
package my1;

public class Course {

    private String cname;

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}

package my1;

import java.util.List;

public class list {
    private List<Course> list;

    public void setList(List<Course> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}

xml配置

<!--    创建多个course对象-->
    <bean id="course" class="my1.Course">
        <property name="cname" value="spring框架"></property>
    </bean>

    <bean id="course1" class="my1.Course">
        <property name="cname" value="sql"></property>
    </bean>

    <bean id="course2" class="my1.Course">
        <property name="cname" value="java"></property>
    </bean>

<bean id="list" class="my1.list">
    <property name="list">
        <list>
            <ref bean="course"></ref>
            <ref bean="course1"></ref>
            <ref bean="course2"></ref>
        </list>
    </property>
</bean>

测试

package my1;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        list list1=context.getBean("list", list.class);
        list1.test();
    }
}

 运行成功

 

举报

相关推荐

0 条评论