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