111
请删除这个帖子
1
yanghanlin 2022-09-11 23:22:37 +08:00 1
private static RestTemplate REST_TEMPLATE = (RestTemplate) SpringUtil.getBean( ...
这里 SpringUtil.getBean 运行时 ApplicationContext 还未建立起来,自然无法获取到 bean |
2
RedBeanIce OP @yanghanlin yes 您是对的。
|
3
RedBeanIce OP |
4
RedBeanIce OP @yanghanlin
这样就行了!!感谢!!! ```java @Component public class RestTemplateUtil implements ApplicationListener<ApplicationStartedEvent>, ApplicationContextAware { private static RestTemplate REST_TEMPLATE; private ApplicationContext applicationContext; private RestTemplateUtil() { } public void setApplicationContext(ApplicationContext inputApplicationContext) throws BeansException { this.applicationContext = inputApplicationContext; } public void onApplicationEvent(ApplicationStartedEvent event) { RestTemplate restTemplate = (RestTemplate)this.applicationContext.getBean("xuegao-framework-restTemplate", RestTemplate.class); setRestTemplate(restTemplate); log.info("[xue-gao-framework][RestTemplateUtil][onApplicationEvent][设置 restTemplate 完毕]"); } public static void setRestTemplate(RestTemplate restTemplate) { if (REST_TEMPLATE == null) { REST_TEMPLATE = restTemplate; } } ``` |