Spring Notes 8--Value injection of complex object (Map, Array...) properties

A collection of all my spring notes
Spring5 introductory knowledge integration (continuous update)

In the extraordinary business development, we can not just be limited to that of the eight basic types, can not rely on a String and a class on just rice
, there are many relatively complex types such as Array array, Map mapping, collection of Set, List list
below I Try your best to talk one by one (all talks are unrealistic)

1. Environmental preparation

Make some modifications according to the Spring7 template. If you haven’t read it yet, please click on it, and it will take a minute or two (dog head).
Spring Notes 7-Constructors are injected with values ​​in different ways
Insert picture description here

package com.ysj.study;

import java.util.*;

public class User {
    
    

    private String name;
    private int age;
    private List<String> books;
    private String[] friendName;
    private Set<String> backCards;
    private Map<String,String> games;
    private Properties myInfo;

    public String getName() {
    
    
        return name;
    }

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

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public List<String> getBooks() {
    
    
        return books;
    }

    public void setBooks(List<String> books) {
    
    
        this.books = books;
    }

    public String[] getFriendName() {
    
    
        return friendName;
    }

    public void setFriendName(String[] friendName) {
    
    
        this.friendName = friendName;
    }

    public Set<String> getBackCards() {
    
    
        return backCards;
    }

    public void setBackCards(Set<String> backCards) {
    
    
        this.backCards = backCards;
    }

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

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

   
    public Properties getMyInfo() {
    
    
        return myInfo;
    }

    public void setMyInfo(Properties myInfo) {
    
    
        this.myInfo = myInfo;
    }
    
    @Override
    public String toString() {
    
    
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", books=" + books +
                ", friendName=" + Arrays.toString(friendName) +
                ", backCards=" + backCards +
                ", games=" + games +
                ", myInfo=" + myInfo +
                '}';
    }
}


2. List type value injection

Insert picture description here

3. Value injection of Array type

Insert picture description here

4. Value injection of Map type

Insert picture description here
The Map type is a bit different from other types because it is a key-value pair, and its underlying implementation is related to entry. When assigning a Map object, it is best to add key and value (but HashMap supports empty key values)

5. Value injection of Set type

Insert picture description here

6. Value injection of Properties type

Properties is a file type, and the suffix name is the type name.
Insert picture description here
Assign values ​​to the name and age of the object. What I assign is (YSJ and 19). The
output results are as follows
Insert picture description here

At the same time remind me: Spring does not support Queue, Vector, Stack and other data types, which is still very restrictive (not relevant)

I believe these dry goods can be almost mastered by taking a few examples after class to test and test.

Guess you like

Origin blog.csdn.net/YSJ367635984/article/details/113388634