Spring IOC Set注入

Spring IOC Set注入–小示例

项目结构如下:

在这里插入图片描述
一个包三个实体类,代码如下:

package com.pojo;
import java.util.List;
public class Student {
    private String name;
    private String sex;
    private int age;
    private List<Phone> phone;
    private List<Subject> submap;
    public void introduce(){
        System.out.println("我叫"+this.name+"性别:"+this.sex+"今年"+this.age+"岁,我来show一下我的手机");
        if (phone != null){
            for (Phone phone1 : phone) {
                phone1.show();
            }
        }
        if (submap != null){
            for (Subject sub : submap) {
                sub.subShow();
                System.out.println(sub.getSubName()+" "+sub.getScore());
            }
        }
    }
    public String getName() { return name;}
    public void setName(String name) {this.name = name;}
    public String getSex() {return sex;}
    public void setSex(String sex) {this.sex = sex;}
    public int getAge() {return age;}
    public void setAge(int age) {this.age = age;}
    public List<Phone> getPhone() {return phone;}
    public void setPhone(List<Phone> phone) {this.phone = phone;}
    public List<Subject> getSubmap() {return submap;}
    public void setSubmap(List<Subject> submap) { this.submap = submap;}
}

package com.pojo;

public class Phone {
    private  String brand;
    private  int price;
    public  void  show(){
        System.out.println("show一下手机");
        System.out.println("品牌:"+this.brand+"价格:"+this.price);
    }
    public String getBrand() {return brand;}
    public void setBrand(String brand) {this.brand = brand;}
    public int getPrice() {return price;}
    public void setPrice(int price) { this.price = price;}
}
package com.pojo;

public class Subject {
    private  String subName;
    private  int score;

    public void subShow(){
        System.out.println("show一下分数");
        System.out.println("科目"+this.subName+"分数"+this.score);

    }


    public String getSubName() {
        return subName;
    }

    public void setSubName(String subName) {
        this.subName = subName;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.pojo.Phone" id="phone1">
        <property name="brand" value="小米10破"/>
        <property name="price" value="4999"/>
    </bean>
    <bean class="com.pojo.Phone" id="phone2">
        <property name="brand" value="iphone11破"/>
        <property name="price" value="9999"/>
    </bean>
    <!-- - - - - - - - - - - - - -->
    <bean class="com.pojo.Subject" id="subject1">
        <property name="subName" value="java"/>
        <property name="score" value="20"/>
    </bean>
    <bean class="com.pojo.Subject" id="subject2">
        <property name="subName" value="C++"/>
        <property name="score" value="18"/>
    </bean>
    <!-- - - - - - - - - - - - - -->
    <bean class="com.pojo.Student" id="student1">
        <property name="name" value="赵铁柱"/>
        <property name="sex" value="男"/>
        <property name="age" value="20"/>
        <property name="phone">
            <list>
                <ref bean="phone1"/>
                <ref bean="phone2"/>
            </list>
        </property>
        <property name="submap">
           <list>
               <ref bean="subject1"/>
               <ref bean="subject2"/>
           </list>
        </property>
    </bean>
    <bean class="com.pojo.Student" id="student2">
        <property name="name" value="王二丫"/>
        <property name="sex" value="女"/>
        <property name="age" value="21"/>
        <property name="phone">
            <list>
                <ref bean="phone1"/>
                <ref bean="phone2"/>
            </list>
        </property>
        <property name="submap">
            <list>
                <ref bean="subject1"/>
                <ref bean="subject2"/>
            </list>
        </property>
    </bean>
</beans>

测试类如下:

package com.test;

import com.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest1 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
        Student student1 = context.getBean("student1", Student.class);
        Student student2 = context.getBean("student2", Student.class);
        student1.introduce();
        student2.introduce();
    }
}

结果如下:

我叫赵铁柱性别:男今年20,我来show一下我的手机
show一下手机
品牌:小米10破价格:4999
show一下手机
品牌:iphone11破价格:9999
show一下分数
科目java分数20
java 20
show一下分数
科目C++分数18
C++ 18
我叫王二丫性别:女今年21,我来show一下我的手机
show一下手机
品牌:小米10破价格:4999
show一下手机
品牌:iphone11破价格:9999
show一下分数
科目java分数20
java 20
show一下分数
科目C++分数18
C++ 18

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值