繁体   English   中英

ClassLoader 找不到我的属性文件

[英]ClassLoader failed locating my properties file

my problem is when i use this code in IDE such as eclipse it works well, but when i use it in tomcat server it doesn't work this is a part of java servlet application (i don't use maven or spring... )

public class DAOFactory {
    private final static String PROPERTY_DRIVER = "driver";
    private final static String PROPERTY_USER_NAME = "userName";
    private final static String PROPERTY_PASSWD = "passwd";
    private final static String PROPERTY_DB_NAME = "dbName";
    private final static String PROPERTY_URL = "url";
    private final static String FILE_PROPERTIES = "/com/DAO/dbInfos.properties";
    
    private String url;
    private String passwd;
    
    private String userName;
    
    /**
     * 
     * @param url
     * @param passwd
     * @param userName
     */
    public DAOFactory(String url,String passwd,String userName)
    {
        this.url = url;
        this.passwd = passwd;
        this.userName = userName;
    }
    /**
     * 
     * @return
     * @throws DAOConfigurationException
     */
    public static DAOFactory getInstance()throws DAOConfigurationException
    {
        Properties properties = new Properties();
        String passwd;
        String url;
        String userName;
        String driver ;

        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream input  = loader.getResourceAsStream(FILE_PROPERTIES);
        
        if(input == null)
            throw new DAOConfigurationException("properties file not exist EXCEPTION");
        try
        {
            properties.load(input);
            
            url = properties.getProperty(PROPERTY_URL);
            passwd = properties.getProperty(PROPERTY_PASSWD);
            
            userName = properties.getProperty(PROPERTY_USER_NAME);
            driver = properties.getProperty(PROPERTY_DRIVER);   
        }
        catch(IOException e)
        {
            throw new DAOConfigurationException("properties file loading EXCEPTION ",e);
        }
        
        try
        {
            Class.forName(driver);
        }
        catch(ClassNotFoundException e)
        {
            throw new DAOConfigurationException("------------- driver loading error ",e);
        }
        
        return new DAOFactory(url,passwd,userName);
    }
    
    public Connection getConnection()throws DAOConfigurationException
    {
        try
        {
            return DriverManager.getConnection(url,userName,passwd);
        }catch(SQLException e)
        {
            throw new DAOConfigurationException("connection failed to be created",e);
        }
    }
    
    /**
     * 
     * @return
     */
    public UniteDAO getUnite()
    {
        return new UniteImplDAO(this);
    }
}

总是在上面的 if 语句中抛出“properties file not exist EXCEPTION”消息

package 的层次结构是

src
 |_ com
    |_ DAO
        |_ DAOFactory
        |_ dbInfos.properties 
WEB-INF
  |_ 
      .
      .
      .

请你帮忙:)

类加载器的getResourceAsStream方法不能以斜杠开头。

但是,这通常不是正确的 gRAS 方法。 您几乎总是想要的(因为线程上下文加载器通常不是您认为的那样,甚至可以是null ),是一个类的加载器:

DAOFactory.class.getResourceAsStream(FILE_PROPERTIES);

是你真正想要的,而这个确实想要那个前导斜线! 这会在类路径上存在的同一个位置查找您想要的文件DAOFactory.class ,例如在同一个 jar 或诸如此类中。 If you don't want to look in the place that class exists, then pass as parameter a java.lang.Class object as 'context' for where you should be looking. 线程上下文加载器不是一些“只是神奇地知道在哪里看”的逃避。

你的代码也充满了错误和风格的假通行证:

  1. 您没有使用 try-with-resources 来安全地关闭您的资源。
  2. 您不遵守命名约定。 它是DaoFactory ,而不是 DAOFactory 和DaoConfigurationException ,而不是DAOConfigurationException
  3. 全大写没有例外。 这是一个例外,“呃,出事了,小心、错误、警报”,这是它与生俱来的本性。 您不需要感叹号或全部大写来进一步突出这一点。
  4. Class.forName(driver); 不需要,并且已经有 20 年没有了。
  5. SQLException包装到DaoConfigurationException以打开连接是不正确的:也许您的配置没有问题,但服务器本身存在问题(例如,它已达到最大同时连接数)。 More generally if the method's definition inherently ties itself to SQL (which does one does; what do you think a method named getConnection that returns a java.jdbc.Connection object, in class DaoFactory , does? The answer obviously includes 'SQL stuff'! ) - 那么该方法应该被声明为throws SQLException ,并且没有必要包装它,尤其是将它包装在一个名称可能是谎言的类型中(它可能根本不是配置问题)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM