栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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依赖
        
        
            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文件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890822.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号