新建一个springboot项目,写两个类
person.java
package com.shrimpking;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Date;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/12/22 10:21
*/
@Data
@ConfigurationProperties(prefix = "person")
public class Person
{
private String name;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private String[] lists;
private Dog dog;
}
dog.java
package com.shrimpking;
import lombok.Data;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/12/22 10:22
*/
@Data
public class Dog
{
private String name;
private Integer age;
}
application.yml
server:
port: 8089
person:
name: zhangsan
age: 18
boss: false
birth: 2023/01/01
maps: {k1: v1, k2: v2}
lists:
- lisi
- zhaoliu
- wangwu
dog:
name: tom
age: 2
启动类
package com.shrimpking;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(Person.class)
@SpringBootApplication
public class Springboot76Application
{
public static void main(String[] args)
{
SpringApplication.run(Springboot76Application.class, args);
}
}
controller
package com.shrimpking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/12/22 10:28
*/
@RestController
public class Controller1
{
@Autowired
private Person person;
@GetMapping("/test")
public String test(){
return person.toString();
}
}