SpringBoot如何将自建的对象纳入spring容器
2019-11-26 本文已影响0人
喵星人ZC
一、创建Student、Grade对象
Pom添加依赖
<!--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实战
二、单元测试
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);
}
}
image.png