代码之家  ›  专栏  ›  技术社区  ›  Robin

弹簧引导自动布线BeanCreationException for Static Field

  •  1
  • Robin  · 技术社区  · 7 年前

    当我自动连接我的属性类时,我遇到了一个问题。

    下面是我的properties类,当我在@service类中自动连接它时,它的行为很好。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Component;
    
    @Component
    @RefreshScope
    public class SMSHistoryProperties {
    
        @Autowired
        Environment env;
    
        /**
         * Return the property value associated with the given key, or null if the
         * key cannot be resolved.
         * 
         * @param propName
         * @return
         */ 
        public String getProperty(String propName){     
            return env.getProperty(propName);
        }
    

    但是当我在我的sqlconstants类(它只有静态常量变量)中自动连接它时,我得到了一个异常。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.vzw.onem.search.properties.SMSHistoryProperties;
    
    @Component
    public class SQLConstants {
    
        @Autowired
        private static SMSHistoryProperties prop;
    
        //~ Static fields/initializers ------------------------------------------
    
        private static final String SMS_SCHEMA = prop.getProperty("sms.schema");
    

    我得到的例外情况如下:

    org.springframework.beans.factory.BeanCreationException: Error
    creating bean with name 'SQLConstants' defined in file
    [...\SQLConstants.class]: Instantiation of bean failed; nested
    exception is org.springframework.beans.BeanInstantiationException:
    Failed to instantiate [com.search.constants.SQLConstants]: Constructor
    threw exception; nested exception is java.lang.NullPointerException
    

    编辑:

    我取出了静态的最终引用,但仍然得到一个空指针异常。

    @Autowired
    private SMSHistoryProperties prop;
    
    private String BATCH_SMS_SQL = "SELECT ACK_CODE FROM "
                + prop.getProperty("sms.schema");
    

    这个 nullpointer 发生在 prop.getProperty("sms.schema") .

    org.springframework.beans.factory.unsuspeedPendencyException:未满足的待处理异常: 创建名为“smscontroller”的bean时出错:依赖项不满意 通过字段“smsbuilder”表示;嵌套异常为 org.springframework.beans.factory.unsuspeedPendencyException:未满足的待处理异常: 创建名为“smsbuilder”的bean时出错:不满足的依赖项 通过字段“searchhelper”表示;嵌套异常为 org.springframework.beans.factory.unsuspeedPendencyException:未满足的待处理异常: 创建名为“smsjdbcsearchhelper”的bean时出错:未满足 通过字段“cccdao”表示的依赖项;嵌套异常为 org.springframework.beans.factory.beanCreationException:错误 正在创建文件中定义的名为“smssearchdaoimpl”的bean [..\smssearchdaoimpl.class]:bean的实例化失败;嵌套 异常为org.springframework.beans.bean InstantiationException: 未能实例化[com.vzw.onem.search.dao.impl.smssearchdaoimpl]: 构造函数引发异常;嵌套异常为 java.lang.NullPointerException(空指针异常)

    2 回复  |  直到 7 年前
        1
  •  4
  •   davidxxx    7 年前

    static

    @PostConstruct

    @Component
    public class SQLConstants {
    
       @Autowired
       private SMSHistoryProperties prop;
    
       //~ Static fields/initializers ------------------------------------------
    
       private String SMS_SCHEMA; 
    
       @PostConstruct
       public void init(){
            SMS_SCHEMA = prop.getProperty("sms.schema"); 
       }
    }
    

    final

    private final String schema;   
    
    public SQLConstants(@Value("${sms.schema}")String schema){
        this.schema = schema;
    }
    

    @Value("${sms.schema}")
    private String schema;   
    

    SMSHistoryProperties

    @Component
    @RefreshScope
    public class SMSHistoryProperties {
    
        @Autowired
        Environment env;
    
        public String getSchema(){     
            return env.getProperty("sms.schema");
        }
    
        public String getOtherValue(){     
            return env.getProperty("sms.otherValue");
        }
    }
    
        2
  •  2
  •   Amit Bera    7 年前

    Autowired Spring SMSHistoryProperties prop null prop getProperty("sms.schema"); NullPointerException