How does SpringBoot incorporate self-built objects into the spring container

1. Create Student and Grade objects

Pom add dependencies

<!--spring 配置依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

  <!--spring单元测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

Student

package com.soul.java.springboot.domain;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    
    
    private String id;
    private String name;
    private Date birthday;
    private boolean male;
    private Map<String, String> infos;
    private List<String> books;
    private Grade grade;

    @Override
    public String toString() {
    
    
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", birthday=" + birthday +
                ", male=" + male +
                ", infos=" + infos +
                ", books=" + books +
                ", grade=" + grade +
                '}';
    }

    public String getId() {
    
    
        return id;
    }

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

    public String getName() {
    
    
        return name;
    }

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

    public Date getBirthday() {
    
    
        return birthday;
    }

    public void setBirthday(Date birthday) {
    
    
        this.birthday = birthday;
    }

    public boolean isMale() {
    
    
        return male;
    }

    public void setMale(boolean male) {
    
    
        this.male = male;
    }

    public Map<String, String> getInfos() {
    
    
        return infos;
    }

    public void setInfos(Map<String, String> infos) {
    
    
        this.infos = infos;
    }

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

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

    public Grade getGrade() {
    
    
        return grade;
    }

    public void setGrade(Grade grade) {
    
    
        this.grade = grade;
    }
}

Grade

package com.soul.java.springboot.domain;


import org.springframework.stereotype.Component;

@Component
public class Grade {
    
    
    private String name;
    private String desc;

    @Override
    public String toString() {
    
    
        return "Grade{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }

    public String getName() {
    
    
        return name;
    }

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

    public String getDesc() {
    
    
        return desc;
    }

    public void setDesc(String desc) {
    
    
        this.desc = desc;
    }
}

application.yaml

student:
  id: 1
  name: 皇家马德里
  birthday: 2015/15/21
  male: true
  infos:
   father: 皇某
   mother: 马某
  books:
   - 深入浅出之Spark
   - 深入浅出之Flink
   - 深入浅出之Hadoop
  grade:
   name: G6
   desc: 深入浅出之Spark实战

2. Unit testing

package com.soul.java.springboot;


import com.soul.java.springboot.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloSpringApplicationTest {
    
    

    @Autowired
    private Student student;

    @Test
    public void test01(){
    
    
        System.out.println(student);
    }
}

output result
insert image description here

Guess you like

Origin blog.csdn.net/qq_27480007/article/details/130478253