大三上寒假生活打卡023

在springboot项目中的resources目录下新建一个文件 application.yml

编写一个实体类 Dog;

复制代码
package com.example.springboot02configure.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
//添加到spring组件中
public class Dog {
    private String name;
    private Integer age;
}
复制代码

编写一个person类

复制代码
package com.example.springboot02configure.pojo;


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 = "person")
public class Person {

    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

}
复制代码

在yaml中写入对象

复制代码
Person:
  name: xiaoqi
  age: 13
  happy: false
  birth: 2009/01/15
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - dog
  dog:
    name: qq
    age: 1
复制代码

在测试程序中测试

复制代码
package com.example.springboot02configure;

import com.example.springboot02configure.pojo.Dog;
import com.example.springboot02configure.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.swing.*;

@SpringBootTest
class SpringBoot02ConfigureApplicationTests {
    @Autowired
    private Person person;


    @Test
    void contextLoads() {
        System.out.println(person);
    }

}
复制代码

 

posted @   软件拓荒人  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示