Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
静态块中的Java ExceptionInitializationError_Java_Spring_Spring Boot - Fatal编程技术网

静态块中的Java ExceptionInitializationError

静态块中的Java ExceptionInitializationError,java,spring,spring-boot,Java,Spring,Spring Boot,我试图在静态块内调用URL检索方法,bean的实例化失败;嵌套异常为java.lang.ExceptionInInitializeError 我正在尝试从配置文件获取WSDL url。此配置数据存储在数据库中 static { URL url = null; WebServiceException e = null; try { url = getVertexConfiguration(); } catch (MalformedURLException

我试图在静态块内调用URL检索方法,bean的实例化失败;嵌套异常为java.lang.ExceptionInInitializeError

我正在尝试从配置文件获取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);
    }

}
静态
{
URL=null;
WebServiceException e=null;
尝试
{
url=getVertexConfiguration();
}捕获(格式错误){
e=新的WebServiceException(ex);
}
}
私有静态URL getVertexConfiguration()引发格式错误的异常
{
试一试{
configuration=configurationDAO.getByRefName(“税/顶点”,
SecureSession.getUser().getDataDomain()!=null?
SecureSession.getUser().getDataDomain():“app.canta”);
}捕获(B2B交易失败B2B交易失败){
}
Map vertexTaxConfig=configuration.getConfigs();
vertexWsdlUrl=vertexTaxConfig.get(“vertexWsdlUrl”).getValue().toString();
返回新的URL(vertexWsdlUrl);
}
}

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

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

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

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

如果静态初始值设定项块中存在一些错误,则会得到ExceptioninInitializerBlock。您只处理
畸形异常
,但可能还有其他异常

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

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

根本原因是静态块是设置为类级初始化的最早步骤,甚至在构造函数调用之前。也就是说,静态块中的依赖项,例如
configurationDAO
尚未初始化。你不应该用静电。相反,您应该使其成为一个普通的实例方法

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

从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
中放置一个try catch on
Throwable
,并让catch块打印出堆栈:

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

我正在自动连接configurationDAO,如下所示@Autowired static configurationDAO configurationDAO@ArundhathiD为什么将bean自动关联为静态?@ArundhathiD如何初始化此bean?如何在静态中初始化ConfigurationDaoblock@ArundhathiD您不应该这样做。在类初始化期间,不要做任何可能导致此类错误的事情;将这些内容放在构造函数中。(它甚至不应该是静态的;您可能希望支持多个副本,例如用于测试。)