Spring的依赖注入

1.准备工作

 Address类

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

User类

import java.util.*;

public class User {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    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 Properties getInfo() {
        return info;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                '}';
    }
}

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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="address" class="com.pojo.Address"></bean>

    <bean id="user" class="com.pojo.User">
        <!--第一种,普通值注入,value-->
        <property name="name" value="Jerry"></property>
        <!--第二种,Bean注入,ref-->
        <property name="address" ref="address"></property>

        <!--数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <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="123456789123456"></entry>
                <entry key="银行卡" value="123456789123456"></entry>
            </map>
        </property>

        <!--Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>DNF</value>
            </set>
        </property>

        <!--null-->
        <property name="wife">
            <null></null>
        </property>

        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">8204000</prop>
                <prop key="性别">男</prop>
                <prop key="username">Tom</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

    </bean>
</beans>

2.测试实现类

import com.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
        System.out.println(user.toString());
    }
}

 

posted on 2021-10-12 11:00  拾柒的  阅读(36)  评论(0)    收藏  举报