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);
相关推荐
Vex2une11 分钟前
JDK 8 下载安装与环境变量配置完整指南(Windows新手图文教程)
java·windows·环境配置·环境变量·jdk8·jdk下载安装·java入门教程
文丑颜不良啊1 小时前
LeetCode3281~3300题目解析
java·leetcode
nice_lcj5202 小时前
【从零理解计算机网络 · 02】分层模型:别背 OSI 七层,看数据怎么被“打包“和“拆包“
java·后端·tcp/ip·计算机网络·面试·osi·分层模型
SimonKing3 小时前
给Deepseek Harness装上插件市场,从此插件无忧
java·后端·程序员
艾莉丝努力练剑3 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
aiotto资源分享4 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
RuoyiOffice4 小时前
SpringBoot3+Vue3 采购到付款:ERP、WMS与财务如何按同一来源协同
java·spring boot·架构·vue
文丑颜不良啊5 小时前
LeetCode3261~3280题目解析
java·leetcode
代码不停5 小时前
Spring 原理
java·spring
企业数字化笔记5 小时前
报表上线怎么对账?口径回归、权限抽检与差异定位
java·spring boot·mysql