spring的set注入

各种类型的注入方式都大同小异,只需注意Map类型和Properties类型

建一个学生类

public class Student {
    private String name;
    private usertwo usert;
    private String[] books;

    public List<String> getSports() {
        return sports;
    }

    private List<String> sports;
    private Map<String,String> card;
    private Set<String> games;
    private String girlfriend;
    private Properties info;

    public void setName(String name) {
        this.name = name;
    }

    public void setUsert(usertwo usert) {
        this.usert = usert;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }


    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public void setGirlfriend(String girlfriend) {
        this.girlfriend = girlfriend;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getName() {
        return name;
    }

    public usertwo getUsert() {
        return usert;
    }

    public String[] getBooks() {
        return books;
    }

    public void setSports(List<String> sports) {
        this.sports = sports;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public Set<String> getGames() {
        return games;
    }

    public String getGirlfriend() {
        return girlfriend;
    }

    public Properties getInfo() {
        return info;
    }
}

在spring的配置文件中注入数据

 <bean id="student" class="com.zheng.Student">
        <!--普通值注入 value-->
        <property name="name" value="zwl"/>
        <!--Bean注入,ref-->
        <property name="usert" ref="usert"/>
        <!--数组的注入,ref-->
        <property name="books">
            <array>
                <value>火影忍者</value>
                <value>海贼王</value>
                <value>死神</value>
            </array>
        </property>
        <!--list注入-->
        <property name="sports">
            <list>
                <value>篮球</value>
                <value>足球</value>
                <value>羽毛球</value>
            </list>
        </property>
      <!--Map注入-->
        <property name="card">
            <map>
                <entry key="学生证" value="156664"/>
                <entry key="身份证" value="624756200007140015"/>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>上古卷轴</value>
                <value>巫师</value>
            </set>
        </property>
        <!--null注入-->
        <property name="girlfriend">
            <null/>
        </property>
        <!--properties注入-->
        <property name="info">
            <props>
                <prop key="学号">20201030</prop>
                <prop key="性别"></prop>
                <prop key="uername">root</prop>
                <prop key="password">123</prop>
            </props>
        </property>
 </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值