Springboot资源文件属性配置(五)

目录

一、目标

二、spring-boot-configuration-processor生成元数据

三、映射到实体步骤

1、pom依赖

2、在资源目录下新建配置文件

3、写Configuration配置类,使用注解

4、新建用于copy的实体类Company

5、新建controller类 CompanyController

6、保存代码,代码热部署,请求接口http://localhost:8080/company/getProperties

7、那么我们的元数据文件在生成到哪里了呢?如下图所示,编译之后的文件,会发现多了一个spring-configuration-metadata.json文件


一、目标

        将资源文件中的属性配置映射到实体类

二、spring-boot-configuration-processor生成元数据

为什么在IDEA中使用SpringBoot的时候, 我们在配置文件中总能在输入spring时会得到很多的输入提示?如下图:

这是因为Spring的项目中 (我们依赖的jar) 包含了很多元数据,这些元数据存储在一个json文件中spring-configuration-metadata.json,如下图所示:

以上的功能实现就是依赖spring-boot-configuration-processor实现。

三、映射到实体步骤

1、pom依赖

        <!--配置处理器,用于生成元数据-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

2、在资源目录下新建配置文件

common.company.name=hik
common.company.address=hangzhou
common.company.phone=07898932

3、写Configuration配置类,使用注解

@Configuration//声明这是个配置类
@ConfigurationProperties(prefix = "common.company")//配置的前缀
@PropertySource("classpath:company.properties")//配置文件路径
package com.example.springbootdemo.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ConfigurationProperties(prefix = "common.company")
@PropertySource("classpath:company.properties")
public class CompanyConfig {

    private String name;

    private String address;

    private String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

 如图所示:

4、新建用于copy的实体类Company

package com.example.springbootdemo.bean;

import org.springframework.stereotype.Component;

@Component
public class Company {

    private String name;

    private String address;

    private String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

5、新建controller类 CompanyController

package com.example.springbootdemo.controller.user;

import com.example.springbootdemo.bean.Company;
import com.example.springbootdemo.common.BaseResult;
import com.example.springbootdemo.configuration.CompanyConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Resource
    private CompanyConfig companyConfig;


    @GetMapping("/getProperties")
    public BaseResult getProperties(){
        Company company = new Company();
        BeanUtils.copyProperties(companyConfig, company);
        return BaseResult.success(company);
    }
}

6、保存代码,代码热部署,请求接口http://localhost:8080/company/getProperties

7、那么我们的元数据文件在生成到哪里了呢?如下图所示,编译之后的文件,会发现多了一个spring-configuration-metadata.json文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值