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>
上一篇:SQLite3问题


下一篇:JAVA 例子