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;
    }
}

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

猜你喜欢

转载自www.cnblogs.com/daleyzou/p/noAutowiredBean.html