Spring的set注入

创建实体类

package com.stewart.pojo;

import java.util.*;

public class Student {
    private String name;
    private Adderss adderss;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Adderss getAdderss() {
        return adderss;
    }

    public void setAdderss(Adderss adderss) {
        this.adderss = adderss;
    }

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

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

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

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

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

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

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

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", adderss=" + adderss +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
  
package com.stewart.pojo;

public class Adderss {
    private String adderss;


    public String getAdderss() {
        return adderss;
    }

    public void setAdderss(String adderss) {
        this.adderss = adderss;
    }

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

beans.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-3.0.xsd">
    
    <bean id="adderss" class="com.stewart.pojo.Adderss"/>


    
    <bean id="student" class="com.stewart.pojo.Student">

        <!--第一种。普通值注入,value-->
        <property name="name" value="你好"/>
        
        <!--Bean注入 ref-->
        <property name="adderss" ref="adderss"/>

        <!-- 数组注入  ref  -->

        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>三国</value>
                <value>西游记</value>
            </array>
        </property>

        <!--list-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看书</value>
                <value>看电影</value>
            </list>
        </property>
        <!--map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="22222222"/>
                <entry key="银行卡" value="333333"/>

            </map>
        </property>

        <!--set-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>lgg</value>
                <value>ldd</value>
            </set>
        </property>
        
        <!--null-->
        <property name="wife">
            <null/>
        </property>

        <property name="info">
            <props>
                <prop key="学号">333</prop>
                <prop key="性别">男</prop>
                <prop key="项目">你好</prop>
            </props>
        </property>
     </bean>

</beans>

Test

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

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

猜你喜欢

转载自www.cnblogs.com/-stewart/p/12507402.html
今日推荐