Springboot customize the yml configuration file and get the value

Foreword:
Since the company wants to call the third-party interface, the third-party interface is divided into two environments (test/formal), so the request path is different; of
course , our development environment must call the test environment for development, and call the formal environment online; to ensure that the environment When the code is not changed, then we can only start with the configuration file;

The realization process:
1.yml configuration file:
Insert picture description here

xufan:
  getPrizeConversionUrl: https://blog.csdn.net/weixin_44146379

2. Mapping entity objects (multiple configuration fields may be set in the project, so I created an object here):
Insert picture description here
Note:
@Value reads the configuration file
@Component is used on the class or interface to specify the scan path, spring will Automatically assemble the classes with the specified annotations under the specified path into the bean container

package com.king.science.dto;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author xf
 * @version 1.0.0
 * @ClassName GetPrizeConversionUrlConfig
 * @Description TODO
 * @createTime 2020.08.04 16:45
 */
@Data
@Component
public class GetPrizeConversionUrlConfig implements Serializable {
    
    

    @Value("${xufan.getPrizeConversionUrl}")
    private String getPrizeConversionUrl;
    
}

3. Take out and use:
Insert picture description here
Effect display:
Insert picture description here
You don’t have to envy what others have, as long as you work hard, you will also have it

Guess you like

Origin blog.csdn.net/weixin_44146379/article/details/107790954