【问题标题】:Java ExceptionInitializationError in static Block静态块中的 Java ExceptionInitializationError
【发布时间】:2019-05-30 14:30:12
【问题描述】:

我正在尝试调用静态块内的 URL 检索方法,bean 的实例化失败;嵌套异常是 java.lang.ExceptionInInitializerError。

我正在尝试从配置文件中获取 WSDL url。此配置数据存储在数据库中。

static 
{
    URL url = null;
    WebServiceException e = null;
    try 
{
    url = getVertexConfiguration();
    } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
    }

private static URL getVertexConfiguration() throws MalformedURLException
    {
        try {
            configuration = configurationDAO.getByRefName("tax/vertex",
                    SecureSession.getUser().getDataDomain() != null ?
                            SecureSession.getUser().getDataDomain() : "app.cantata");
        } catch (B2BTransactionFailed b2BTransactionFailed) {

        }

        Map<String, DynamicAttribute> vertexTaxConfig = configuration.getConfigs();
        vertexWsdlUrl = vertexTaxConfig.get("vertexWsdlUrl").getValue().toString();

        return new URL(vertexWsdlUrl);
    }

}

我得到静态块,bean 的实例化失败;嵌套异常是 java.lang.ExceptionInInitializerError。

【问题讨论】:

  • 在类初始化期间不要做任何可能导致此类错误的事情;把这些东西放在构造函数中。 (它甚至不应该是静态的;您可能希望支持多个副本,例如用于测试。)

标签: java spring spring-boot


【解决方案1】:

根本原因是静态块是设置为类级别初始化的最早步骤,甚至在构造函数调用之前。也就是说你在静态块中的依赖,比如configurationDAO,还没有初始化。你不应该使用静态它。相反,您应该将其设为普通的 Instance 方法。

【讨论】:

    【解决方案2】:

    你为什么要试试这个?我认为您的 configurationDAO 在您尝试访问它时甚至还没有初始化。

    正如我们在 cmets 中所讨论的,我绝对会建议您,作者,正确地注入您的依赖项,例如:

    @Service
    public class ConfigurationService {
    
        private final ConfigurationDao configurationDao;
        private URL url;
    
        public ConfigurationService(ConfigurationDao configurationDao) {
            this.configurationDao = configurationDao;
        }
    
        @PostConstruct
        private void init() {
            // your stuff here
        }
    }
    

    或者你甚至可以在构造函数中初始化你的url

    @Service
    public class ConfigurationService {
    
        private final ConfigurationDao configurationDao;
        private final URL url;
    
        public ConfigurationService(ConfigurationDao configurationDao) {
            this.configurationDao = configurationDao;
            this.url = getVertexConfiguration();
        }
    
        private URL getVertexConfiguration() {
            // your stuff here
        }
    }
    

    【讨论】:

    • 我正在自动装配 configurationDAO,如下所示@Autowired static ConfigurationDAO configurationDAO;
    • @ArundhathiD 为什么要将 bean 自动装配为静态?
    • @ArundhathiD 你如何初始化这个 bean?
    • 如何在静态块中初始化 ConfigurationDao
    • @ArundhathiD 你不应该这样做
    【解决方案3】:

    如果静态初始化程序块中有一些错误,您会得到 ExceptioninInitializerBlock。你只处理MalformedURLException,但是,可能还有其他人。

    您应该为所有异常添加另一个catch,看看那里会发生什么。

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = getVertexConfiguration();
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        } catch (Exception e) {
            //real problem
        }
    }
    

    【讨论】:

      【解决方案4】:

      该异常是运行静态初始化程序时发生的错误的中继机制。异常应该有一个原因,它将描述实际的错误。根据您的描述,上面看起来有三层异常:bean 初始化的错误报告,ExceptionInInitializer 异常,然后是ExceptionInInitializer 异常的原因。异常处理应该显示所有三个层,但可能不显示,这将使发现基本异常更加困难。

      来自 ExceptionInInitializer javaDoc:

       * Signals that an unexpected exception has occurred in a static initializer.
       * An <code>ExceptionInInitializerError</code> is thrown to indicate that an
       * exception occurred during evaluation of a static initializer or the
       * initializer for a static variable.
      

      作为备用方案,您可以在 getVertexConfiguration 中放入 Throwable 的 try-catch,然后让 catch 块打印出堆栈:

      private static URL getVertexConfiguration() throws MalformedURLException {
          try {
              // Code omitted
          } catch ( Throwable th ) {
              th.printStackTrace();
              return null;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 2014-06-05
        • 2012-12-09
        • 1970-01-01
        • 2013-09-25
        • 2010-09-08
        相关资源
        最近更新 更多