大三上寒假生活打卡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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现