![](/img/trans.png)
[英]Write to properties file on Wildfly which is loaded by 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. 线程上下文加载器不是一些“只是神奇地知道在哪里看”的逃避。
你的代码也充满了错误和风格的假通行证:
DaoFactory
,而不是 DAOFactory 和DaoConfigurationException
,而不是DAOConfigurationException
。Class.forName(driver);
不需要,并且已经有 20 年没有了。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.