RedBeanIce

111

  •  
  •   RedBeanIce · Sep 11, 2022 · 1000 views
  •   You need to sign in to view this topic
    This topic created in 1514 days ago, the information mentioned may be changed or developed.

    111

    请删除这个帖子

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

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

    感谢您的回复
    RedBeanIce
    RedBeanIce
    OP
    Sep 11, 2022❤️1
    4
    @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   ·   Privacy   ·   Solana   ·   4867 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:52 · PVG 14:52 · LAX 22:52 · JFK 01:52
    ♥ Do have faith in what you're doing.