SpringBoot读取properties配置文件中的数据(亲测)

167 篇文章 6 订阅

常用三种方法:
1、@Value注解读取
2、使用Environment读取
3、使用@ConfigurationProperties注解读取

代码:
链接:https://pan.baidu.com/s/1g56ZUu5Xkf07pNqyHprZBw
提取码:d155

重点说下第三种方法,@ConfigurationProperties项目中用的比较多,其他两种参考:
https://blog.csdn.net/dkbnull/article/details/81953190?spm=1001.2101.3001.6650.3&depth_1-

在src\main\resources下新建config.properties配置文件:

demo.phone=10086
demo.wife=self
1
2
ConfigBeanProp文件

@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {
    private String phone;
    private String wife;
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getWife() {
        return wife;
    }
    public void setWife(String wife) {
        this.wife = wife;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component 表示将该类标识为Bean
@ConfigurationProperties(prefix = “demo”)用于绑定属性,其中prefix表示所绑定的属性的前缀。
@PropertySource(value = “config.properties”)表示配置文件路径。

@RestController
public class GatewayController {
    @Value("${demo.name}")
    private String name;

    @Value("${demo.age}")
    private String age;

    @Autowired
    private ConfigBeanValue configBeanValue;

    @Autowired
    private Environment environment;
    
    @Autowired
    private ConfigBeanProp configBeanProp;

    @RequestMapping(value = "/gateway4")
    public String gateway4() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解读取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age +
                "<p>get properties value by ''Environment'' :" +
                //2、使用Environment读取
                " sex=" + environment.getProperty("demo.sex") +
                " , address=" + environment.getProperty("demo.address") +
                "<p>get properties value by ''@ConfigurationProperties'' :" +
                //3、使用@ConfigurationProperties注解读取
                " phone=" + configBeanProp.getPhone() +
                " , wife=" + configBeanProp.getWife();
    }
}


 


二、扩展

SpringBoot读取配置文件中定义的数组集合

1、application.yml配置文件

navigation:
    platforminfo:
        - key: "fusion"
          name: "大数据融合平台"
        - key: "open"
          name : "开放云服务平台"

navigationbar:
    applinks:
        - name: "数据概览"
          key: "fusion.home"
          url: "/fusion/home/v1/"
        - name: "数据集成"
          key: "fusion.integration"
          url: "/fusion/integration/v1/"
        ...

model:

public class PlatFormInfo {
    private String key;
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private String name;
}

public class AppLinks {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    private String key;
    private String url;
}


43
Properties:

@Component
@ConfigurationProperties(prefix = "navigationbar" )
public class GlobalLinkProperties {
    public List<AppLinks> getAppLinks() {
        return appLinks;
    }
    public void setAppLinks(List<AppLinks> appLinks) {
        this.appLinks = appLinks;
    }
    private List<AppLinks> appLinks = new ArrayList<>();
}


@Component
@ConfigurationProperties(prefix = "navigation" )
public class GlobalNameProperties {

    public List<PlatFormInfo> getPlatFormInfo() {
        return platFormInfo;
    }
    public void setPlatFormInfo(List<PlatFormInfo> platFormInfoList) {
        this.platFormInfo = platFormInfoList;
    }
    private List<PlatFormInfo> platFormInfo = new ArrayList<>();
}

25
RestLoginHeaderResponse:

public class RestLoginHeaderResponse {
    private List<PlatFormInfo> platFormInfoList = new ArrayList<>();
    public List<PlatFormInfo> getPlatFormInfoList() {
        return platFormInfoList;
    }
    public void setPlatFormInfoList(List<PlatFormInfo> platFormInfoList) {
        this.platFormInfoList = platFormInfoList;
    }
    public List<AppLinks> getAppLinksList() {
        return appLinksList;
    }
    public void setAppLinksList(List<AppLinks> appLinksList) {
        this.appLinksList = appLinksList;
    }
    private List<AppLinks> appLinksList = new ArrayList<>();
}


17
测试Controller:

@Controller
public class HeaderController {

    @Autowired
    private GlobalNameProperties globalNameProperties;

    @Autowired
    private GlobalLinkProperties globalLinkProperties;

    @Autowired
    CorsProperties corsProperties;

    @RequestMapping( "/test" )
    @ResponseBody
    public RestResponse<RestLoginHeaderResponse> test(){
        RestResponse<RestLoginHeaderResponse> res = new RestResponse<>();
        RestLoginHeaderResponse response = new RestLoginHeaderResponse();
        List<PlatFormInfo> platFormInfoList = globalNameProperties.getPlatFormInfo();
        List<AppLinks> appLinksList = globalLinkProperties.getAppLinks();
        response.setPlatFormInfoList(platFormInfoList);
        response.setAppLinksList(appLinksList);
        res.setBody(response);
        return res;
    }
}



其中RestResponse是返回前端的response,自己找一个就行
返回json如下:

注意:
ConfigurationProperties 注解用于设置Springboot从配置文件中取值过程中的约束。
定义的List 、List对象名应该和配置文件中的相同。
PlatFormInfo、AppLinks对象的属性名应该和配置文件中的key相同。

即下图要保持一致:

来源:SpringBoot读取properties配置文件中的数据_逍遥浪子~的博客-CSDN博客_springboot 读取properties配置参数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值