当前位置: 技术问答>java相关
请问有关classLoader类的问题?????
来源: 互联网 发布时间:2015-06-30
本文导语: 请问下面带星号的程序是什么意思,实在搞不明白,看过JDK的文档后也是一头雾水,读一个文件为什么要这么麻烦? ----------------------------------------------------------------------- private boolean loadConfig(String name) throws ...
请问下面带星号的程序是什么意思,实在搞不明白,看过JDK的文档后也是一头雾水,读一个文件为什么要这么麻烦?
-----------------------------------------------------------------------
private boolean loadConfig(String name)
throws Exception
{
boolean rc = false;
// Get our class loader
**** ClassLoader cl = getClass().getClassLoader();
java.io.InputStream in;
**** if (cl != null) {
**** in = cl.getResourceAsStream(name);
**** }
**** else {
**** in = ClassLoader.getSystemResourceAsStream(name);
**** }
// If the input stream is null, then the configuration file
// was not found
if (in == null) {
throw new Exception("ConnectionPool configuration file '" +
name + "' not found");
}
else {
try {
m_JDBCProperties = new java.util.Properties();
// Load the configuration file into the properties table
m_JDBCProperties.load(in);
// Got the properties. Pull out the properties that we
// are interested in
m_JDBCDriver = consume(m_JDBCProperties, "JDBCDriver");
m_JDBCConnectionURL = consume(m_JDBCProperties,
"JDBCConnectionURL");
m_ConnectionPoolSize = consumeInt(m_JDBCProperties,
"ConnectionPoolSize");
m_ConnectionPoolMax = consumeInt(m_JDBCProperties,
"ConnectionPoolMax");
m_ConnectionTimeout = consumeInt(m_JDBCProperties,
"ConnectionTimeout");
m_ConnectionUseCount = consumeInt(m_JDBCProperties,
"ConnectionUseCount");
rc = true;
}
finally {
// Always close the input stream
if (in != null) {
try {
in.close();
}
catch (Exception ex) {
}
}
}
}
return rc;
}
-----------------------------------------------------------------------
private boolean loadConfig(String name)
throws Exception
{
boolean rc = false;
// Get our class loader
**** ClassLoader cl = getClass().getClassLoader();
java.io.InputStream in;
**** if (cl != null) {
**** in = cl.getResourceAsStream(name);
**** }
**** else {
**** in = ClassLoader.getSystemResourceAsStream(name);
**** }
// If the input stream is null, then the configuration file
// was not found
if (in == null) {
throw new Exception("ConnectionPool configuration file '" +
name + "' not found");
}
else {
try {
m_JDBCProperties = new java.util.Properties();
// Load the configuration file into the properties table
m_JDBCProperties.load(in);
// Got the properties. Pull out the properties that we
// are interested in
m_JDBCDriver = consume(m_JDBCProperties, "JDBCDriver");
m_JDBCConnectionURL = consume(m_JDBCProperties,
"JDBCConnectionURL");
m_ConnectionPoolSize = consumeInt(m_JDBCProperties,
"ConnectionPoolSize");
m_ConnectionPoolMax = consumeInt(m_JDBCProperties,
"ConnectionPoolMax");
m_ConnectionTimeout = consumeInt(m_JDBCProperties,
"ConnectionTimeout");
m_ConnectionUseCount = consumeInt(m_JDBCProperties,
"ConnectionUseCount");
rc = true;
}
finally {
// Always close the input stream
if (in != null) {
try {
in.close();
}
catch (Exception ex) {
}
}
}
}
return rc;
}
|
就是把你的类.class文件加载到内存中。
这里的代码意思是根据类加载器,可以得到在包中的资源,然后取得相应的信息。
这里的代码意思是根据类加载器,可以得到在包中的资源,然后取得相应的信息。