zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    

    加入前置操作

  • 相关阅读:
    poj 2516 Minimum Cost (最小费用流 )
    new start
    关于c语言中的声明和定义
    多态性与虚函数之对象切片
    C专家编程之typedef
    QGroupBox设置边框
    多态性与虚函数之继承中的virtual 晚捆绑
    使用Map
    遍历控件
    C专家编程之枚举
  • 原文地址:https://www.cnblogs.com/daleyzou/p/noAutowiredBean.html
Copyright © 2011-2022 走看看