RedBeanIce

111

  •  
  •   RedBeanIce · Sep 11, 2022 · 933 views
    This topic created in 1424 days ago, the information mentioned may be changed or developed.

    111

    请删除这个帖子

    yanghanlin
        1
    yanghanlin  
       Sep 11, 2022   ❤️ 1
    private static RestTemplate REST_TEMPLATE = (RestTemplate) SpringUtil.getBean( ...

    这里 SpringUtil.getBean 运行时 ApplicationContext 还未建立起来,自然无法获取到 bean
    RedBeanIce
        2
    RedBeanIce  
    OP
       Sep 11, 2022
    @yanghanlin yes 您是对的。
    RedBeanIce
        3
    RedBeanIce  
    OP
       Sep 11, 2022
    @yanghanlin 但是我不知道有什么办法解决这个问题,这个问题是在 static 的地方,引入 spring 的非 static 对象,,我去尝试一下。,,有一点思路。

    感谢您的回复
    RedBeanIce
        4
    RedBeanIce  
    OP
       Sep 11, 2022   ❤️ 1
    @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;
    }
    }

    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4067 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 05:21 · PVG 13:21 · LAX 22:21 · JFK 01:21
    ♥ Do have faith in what you're doing.