Spring之依赖注入

一、Spring依赖注入方式

创建Address类

public class Address {
    private String address;
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
    public String getAddress() {
        return address;
    }

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

创建Student类

package com.lele.di.pojo;

import java.util.*;

public class Student {

    public Student(String name, Address address, String[] books, List<String> hobbys, Map<String, String> card, Set<String> games, String wifi, Properties info) {
        this.name = name;
        this.address = address;
        this.books = books;
        this.hobbys = hobbys;
        this.card = card;
        this.games = games;
        this.wifi = wifi;
        this.info = info;
    }

    private String name;
    private Address address;
    private String[] books;


    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;


    private String wifi;
    private Properties info;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wifi='" + wifi + '\'' +
                ", info=" + info +
                '}';
    }
    public Map<String, String> getCard() {
        return card;
    }

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

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


    public Student(Map<String, String> card, Set<String> games) {
        this.card = card;
        this.games = games;
    }


    public Student() {

    }

    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 String getWifi() {
        return wifi;
    }

    public void setWifi(String wifi) {
        this.wifi = wifi;
    }

    public Properties getInfo() {
        return info;
    }

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

}

1.普通值注入

<bean id="address" class="com.lele.di.pojo.Address">
        <property name="address" value="西安"/>
</bean>

    <bean id="student" class="com.lele.di.pojo.Student">
    <property name="name" value="JAVATest"/>

2.Bean注入,ref

<property name="address" ref="address"/>

3.数组注入,ref

<property name="books">
     <array>
         <value>红楼梦</value>
         <value>西游记</value>
         <value>水浒传</value>
     </array>
    </property>

4.list入驻

<property name="hobbys">
        <list>
            <value>听歌</value>
            <value>看小说</value>
            <value>看电影</value>
        </list>
    </property>

5.Map入驻

<property name="card">
         <map>
              <entry key="身份证" value="1111111111"/>
             <entry key="银行卡" value="5555555555"/>
         </map>
    </property>

6.Set入驻

<property name="games">
        <set>
            <value>LOL</value>
            <value>COC</value>
            <value>BOB</value>
        </set>
    </property>

7.null

 <property name="wifi">
        <null/>
    </property>

8.Properties

<property name="info">
        <props>
            <prop key="学号">2222222</prop>
            <prop key="性别"></prop>
            <prop key="姓名">小明</prop>
        </props>
    </property>

测试类

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());

    }

输出的结果:

 /*
    Student{
    name='JAVATest',
    address=Address{address='西安'},
    books=[红楼梦, 西游记, 水浒传],
    hobbys=[听歌, 看小说, 看电影],
    card={身份证=1111111111, 银行卡=5555555555},
    games=[LOL, COC, BOB],
    wifi='null',
    info={学号=2222222, 性别=男, 姓名=小明}
    }
  */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值