Configuration 中无法自动注入依赖于component的bean

出现问题时我这样使用依赖注入

@Configuration
public class WebServiceConfig {

    @Autowired
    private IMessageWebService messageWebService;
    
    @Bean
    public Endpoint endpointHttp() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), messageWebService);
        endpoint.publish("/messageWebService");
        return endpoint;
    }
}

出错信息

Caused by: java.lang.NullPointerException: null

方法一

下面这样处理可以解决问题

@Configuration
public class WebServiceConfig {
    
    @Bean
    public Endpoint endpointHttp(IMessageWebService messageWebService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), messageWebService);
        endpoint.publish("/messageWebService");
        return endpoint;
    }
}

我们不使用自动注入,==问题解决==

方法二

@Configuration
@DependsOn(value = "springUtil")
public class WebServiceConfig {

    @Autowired
    private IMessageWebService messageWebService;
    
    @Bean
    public Endpoint endpointHttp() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), messageWebService);
        endpoint.publish("/messageWebService");
        return endpoint;
    }
}

Configuration 中无法自动注入依赖于component的bean

posted @ 2019-03-28 11:23 DaleyZou 阅读(...) 评论(...) 编辑 收藏

相关文章:

  • 2021-12-26
  • 2022-03-08
  • 2021-09-14
  • 2021-06-18
  • 2022-01-01
  • 2021-06-09
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2023-03-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-06
  • 2021-06-25
相关资源
相似解决方案