注解@ConfigurationProperties获取配置文件内容 (demo)

1. 自定义配置文件内容

注解@ConfigurationProperties获取配置文件内容 (demo)_第1张图片

 

2. @ConfigurationProperties

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix="wx-pay.v3")
public class PropertiesVo {

    // appId
    private String appId;
    // 商户id
    private String mchId;
    // APIv3密钥
    private String apiKey3;
    // 证书序列号
    private String serialnumber;
}

3. controller
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试@ConfigurationProperties
 */
@Slf4j
@RestController
public class TestConfigController {


    @Autowired
    private PropertiesVo propertiesVo;

    @RequestMapping("/testProperties")
    public PropertiesVo testConfig() {
        log.info("配置文件信息: {}",propertiesVo);
        return propertiesVo;
    }
}

4. 成功获取配置文件内容

配置文件信息: PropertiesVo(appId=123456789, mchId=666666666, apiKey3=999999, serialnumber=789789)

注解@ConfigurationProperties获取配置文件内容 (demo)_第2张图片

 

你可能感兴趣的:(Annotation,java,开发语言)