ClassLoader 未能找到我的属性文件

问题描述

我的问题是当我在 Eclipse 等 IDE 中使用此代码时它运行良好,但是当我在 tomcat 服务器中使用它时它不起作用这是 java servlet 应用程序的一部分(我不使用 maven 或 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”的消息

包的层次结构是

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

请你帮忙:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)