0

プロパティファイルからデータベースの詳細をロードする以下のクラスがあります。ファイル「dao.properties」が見つからない場合、クラスは DAOConfigurationException をスローする必要がありますが、代わりに取得しています

    javax.servlet.ServletException: Servlet execution threw an exception
root cause

java.lang.ExceptionInInitializerError
    com.clone.dao.DAOFactory.getInstance(DAOFactory.java:19)
    com.clone.controller.register.doPost(register.java:35)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

com.clone.dao.DAOConfigurationException: Properties file dao.properties not found in classpath.
    com.clone.dao.DAOProperties.<clinit>(DAOProperties.java:15)
    com.clone.dao.DAOFactory.getInstance(DAOFactory.java:19)
    com.clone.controller.register.doPost(register.java:35)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

その後のリクエストで、この例外が発生しています

javax.servlet.ServletException: Servlet execution threw an exception
root cause

java.lang.NoClassDefFoundError: Could not initialize class com.clone.dao.DAOProperties
    com.clone.dao.DAOFactory.getInstance(DAOFactory.java:19)
    com.clone.controller.register.doPost(register.java:35)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

これは私のコードです

package com.clone.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DAOProperties {
    private static final String PROPERTIES_FILE = "dao.properties";
    private static final Properties PROPERTIES = new Properties();
<b> static{
        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream propertiesFile = classloader.getResourceAsStream(PROPERTIES_FILE);

        if(propertiesFile==null){
            throw new DAOConfigurationException("Properties file "+PROPERTIES_FILE+" not found in classpath.");
        } 
        try {
            PROPERTIES.load(propertiesFile);
        } catch (IOException e) {
            throw new DAOConfigurationException("Cannot load properties file '"+PROPERTIES_FILE+"'.", e);
        }
    }
</b>
private String specificKey;


    public DAOProperties(String specificKey)
    {
        this.specificKey=specificKey;
    }

    public String getProperty(String key) throws DAOConfigurationException {
        String fullKey = specificKey+"."+key;
        String property = PROPERTIES.getProperty(fullKey);
        if(property==null||property.trim().length()==0){
            throw new DAOConfigurationException("Property '"+fullKey+"' is missing in properties file '"+
                                                        PROPERTIES_FILE+"'.");
        }
        return property;
    }

}

皆さん、何が起こっているのか説明してください。これは非常に紛らわしいです

4

1 に答える 1

3

それ投げていDAOConfigurationExceptionます:

com.clone.dao.DAOConfigurationException: Properties file dao.properties
     not found in classpath.

ただし、を使用しようとしているjava.lang.ExceptionInInitializerErrorため、それは にまとめられています。静的初期化子が失敗したため、クラスを適切に初期化できません。これは、静的初期化子が失敗したときにスローされるものとまったく同じです。DAOFactoryDAOPropertiesDAOPropertiesExceptionInInitializerError

おそらく、静的初期化子内でこれを行うべきではありませんか?

于 2012-11-24T10:28:58.903 に答える