本文主要是介绍SpringBean生命周期之InitializingBean,初始化bean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 yml文件
weixin:appid: aaaaaapartner: 12313214partnerkey: ccccccert: 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;@Overridepublic 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);
这篇关于SpringBean生命周期之InitializingBean,初始化bean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!