SpringBean生命周期之InitializingBean,初始化bean

1 yml文件

复制代码
weixin:
  appid: aaaaaa
  partner: 12313214
  partnerkey: ccccc
  cert: C:\\Users\\lenovo\\Desktop

2 Bean初使化

复制代码
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConstantPropertiesUtils implements InitializingBean {

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.cert}")
    private String cert;

    public static String APPID;
    public static String PARTNER;
    public static String PARTNERKEY;
    public static String CERT;

    @Override
    public void afterPropertiesSet() throws Exception {
        APPID = appid;
        PARTNER = partner;
        PARTNERKEY = partnerkey;
        CERT = cert;
    }
}

3 使用,在spring环境下,直接像工具类那样调用,不需要@Autowired

复制代码
System.out.println("ConstantPropertiesUtils.APPID = " + ConstantPropertiesUtils.APPID);
System.out.println("ConstantPropertiesUtils.PARTNER = " + ConstantPropertiesUtils.PARTNER);
System.out.println("ConstantPropertiesUtils.PARTNERKEY = " + ConstantPropertiesUtils.PARTNERKEY);
System.out.println("ConstantPropertiesUtils.CERT = " + ConstantPropertiesUtils.CERT);
相关推荐
码出财富6 小时前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
我是小疯子666 小时前
Python变量赋值陷阱:浅拷贝VS深拷贝
java·服务器·数据库
森叶7 小时前
Java 比 Python 高性能的原因:重点在高并发方面
java·开发语言·python
二哈喇子!7 小时前
Eclipse中导入外部jar包
java·eclipse·jar
微露清风7 小时前
系统性学习C++-第二十二讲-C++11
java·c++·学习
进阶小白猿7 小时前
Java技术八股学习Day20
java·开发语言·学习
gis开发7 小时前
【无标题】
java·前端·javascript
Wpa.wk7 小时前
性能测试 - 搭建线上的性能测试环境参考逻辑图
java·经验分享·测试工具·jmeter·性能测试
代码村新手8 小时前
C++-类和对象(中)
java·开发语言·c++
葵花楹8 小时前
【JAVA课设】【游戏社交系统】
java·开发语言·游戏