远方蔚蓝
一刹那情真,相逢不如不见

文章数量 126

访问次数 199825

运行天数 1437

最近活跃 2024-10-04 23:36:48

进入后台管理系统

SpringBoot 静态属性注入


使用 @Value() 注解
@Configuration
public class SystemApiConfig {
    private static String account;
    private static String password;
    private static String appid;
    @Value("${system.account}")
    public void setAccouont(String account){
        this.account = account;
    }
    @Value("${system.password}")
    public void setPassword(String password){
        this.password = password;
    }
    @Value("${system.appid}")
    public void setAppid(String appid){
        this.appid = appid;
    }
    public static String getAccount() {
        return account;
    }
    public static String getPassword() {
        return password;
    }
    public static String getAppid() {
        return appid;
    }
}
实现 InitializingBean 接口,重写 afterPropertiesSet() 方法
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SystemApiConfig implements InitializingBean {
    @Value("${system.account}")
    private String account;
    @Value("${system.password}")
    private String password;
    @Value("${system.appid}")
    private String appid;
    public static String ACCOUNT;
    
    public static String PASSWORD;
    
    public static String APPID;
    @Override
    public void afterPropertiesSet() throws Exception {
        ACCOUNT=account;
        PASSWORD=password;
        APPID=appid;
    }
}