1、自定义配置
# 自定义配置
zbj:user:username: rootpassword: 123456# 自定义集合gfs:- a- b- c
2、读取
1)User类
package com.sgu.pojo;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;/*** 满堂花醉三千客,一剑寒霜十四州。** @Author 中瑞* @Date 2025/3/28 14:25*/@Component
@ConfigurationProperties(prefix = "zbj.user")
@Data
public class User {private String username;private String password;private List<String> gfs;
}
2)UserController类
package com.sgu.controller;import com.sgu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 满堂花醉三千客,一剑寒霜十四州。** @Author 中瑞* @Date 2025/3/28 14:26*/@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate User user;@GetMapping("/show")public User show() {return user;}
}
3)访问
启动项目,并在浏览器输入地址:
http://127.0.0.1:8080/user/show
3、参考
145-springboot-批量配置文件读取_哔哩哔哩_bilibili