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

Springboot资源文件属性配置(五),第1张

目录

一、目标

二、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依赖
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
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文件

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/942476.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存