ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring的set注入

2020-11-09 08:32:31  阅读:140  来源: 互联网

标签:set return String Spring void private books public 注入


各种类型的注入方式都大同小异,只需注意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>

标签:set,return,String,Spring,void,private,books,public,注入
来源: https://www.cnblogs.com/zheng-yi/p/13946794.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有