跳至主要內容

读取自定义配置文件属性

mozzie小于 1 分钟Spring BootSpring Boot

创建一个xxxProperties类

@EnableConfigurationProperties
//读取属性配置文件
@ConfigurationProperties(prefix = "wx.open")
@Configuration
@Data
public class SecurityProperties {
    //属性名称与配置文件中的名称对应
    private String appId;
    private String appSecret;
    private String redirectUrl;
}
# 微信开放平台
wx:
  open:
    app_id: xxxxxxxx
    app_secret: xxxxxxxx
    redirect_url: http://sycimi.natappfree.cc/callback

还可以使用value注解

package com.du.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class WxProperties {

    @Value("${wx.open.app_id}")
    private String appId;
    
    @Value("${wx.open.app_secret}")
    private String appSecret;
    
    @Value("${wx.open.redirect_url}")
    private String redirectUrl;
}
贡献者: du