首页 > 编程语言 > 详细

Spring5入门-03-依赖注入和配置的一些细节(bean、数组、list、map、set、properties)

时间:2020-09-23 23:08:44      阅读:67      评论:0      收藏:0      [点我收藏+]

一、前言

目的:去了解xml配置bean依赖注入和配置的一些细节(bean、数组、list、map、set、properties)



二、准备工作

2.1 依赖

注意:这里用到的应该是spring-context,但是spring-webmvc由于继承的关系会有一张依赖网:

技术分享图片

算是省心省力吧。

<!--Spring-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

<!--JUnit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

2.2 实体类

这次使用基于setter的依赖注入,不显式创建构造函数:

路径

技术分享图片

代码

Address.java

package com.duzhuan.pojo;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/23 14:34
 * @Version: 1.0
 * @since: jdk11
 */
public class Address {
    private int id;
    private String address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", address=‘" + address + ‘\‘‘ +
                ‘}‘;
    }
}

Student.java

package com.duzhuan.pojo;

import java.util.*;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/23 14:33
 * @Version: 1.0
 * @since: jdk11
 */
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    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> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    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 "Student{" +
                "name=‘" + name + ‘\‘‘ +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="address,a1" class="com.duzhuan.pojo.Address">
        <property name="id" value="1"/>
        <property name="address" value="上海市翻斗大街翻斗花园二号楼1001室"/>
    </bean>


    <!--注册一个Bean,可以看到,这样的命名方式可以对一个bean命名多个name-->
    <bean name="student,s1" class="com.duzhuan.pojo.Student">
        <property name="name">
            <null/>
        </property>

        <!--注入一个对象-->
        <property name="address" ref="a1"/>

        <!--注入一个数组-->
        <property name="books">
            <array>
                <value>Spring实战(第四版)</value>
                <value>算法导论</value>
            </array>
        </property>

        <!--注入一个List-->
        <property name="hobbies">
            <list>
                <value>写代码</value>
                <value>看番</value>
            </list>
        </property>

        <!--注入一个Map-->
        <property name="card">
            <map>
                <entry key="校园卡" value="20201111003"/>
                <entry key="门禁卡" value="20191111001"/>
            </map>
        </property>

        <!--注入一个set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>WOW</value>
            </set>
        </property>

        <!--注入一个Properties-->
        <property name="info">
            <props>
                <prop key="学号">17123456</prop>
            </props>
        </property>
    </bean>

</beans>

四、 测试样例

import com.duzhuan.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/23 20:41
 * @Version: 1.0
 * @since: jdk11
 */
public class StudentTest {
    @Test
    public void studentTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student s1 = (Student) context.getBean("s1");
        System.out.println(s1);
        
    }
}

技术分享图片

Spring5入门-03-依赖注入和配置的一些细节(bean、数组、list、map、set、properties)

原文:https://www.cnblogs.com/duzhuan/p/13721353.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!