1.定义配置类
java
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
/**
* lecheng云相关配置
*/
@Component
@Data
@RefreshScope
public class LeChangeConfig {
//读取配置文件中的值,并设置默认值.(配置文件如果有值,取配置文件中的值)
@Value("${leChange.url:https://xxxxx/openapi/}")
private String url;
@Value("${leChange.appId:lcxxxxxxeb6}")
private String appId;
@Value("${leChange.appSecret:e7xxxxxxxxxxxx6e}")
private String appSecret;
}
2.配置文件中设置值
java
#配置文件中设置的值
leChange:
url: https://xxxxxxxxx/openapi/
appId: lc0xxxxxxxxxb6
appSecret: e7xxxxxxxxx6e
3.应用该配置
java
@Service
@Slf4j
@AllArgsConstructor
public class LeChangeDeviceServiceImpl implements LeChangeDeviceService {
private final LeChangeConfig leChangeConfig;
@Override
public String getLeChangeAccessToken(){
//注入配置类,获取值
String url = leChangeConfig.getUrl();
return token;
}
}