Apache Commons 配置 - PropertiesConfiguration 已关闭
Apache Commons Configuration - PropertiesConfiguration closed
PropertiesConfiguration.java
没有 close()
方法。是否需要做其他事情来释放文件?在生产中使用它之前,我想确定一下。我查看了代码,但没有看到任何内容。我不完全确定 PropertiesConfiguration.setProperty()
在不打开与随后必须关闭的文件的连接的情况下如何工作。
在 org.apache.commons.configuration.PropertiesConfiguration
中,当在 PropertiesConfiguration
实例中加载属性时,输入(流、路径、url 等...)当然会关闭。
你可以在org.apache.commons.configuration.AbstractFileConfiguration
的void load(URL url)
方法中得到确认。
调用此方法的方式如下:
1) PropertiesConfiguration
构造函数被调用:
public PropertiesConfiguration(File file) throws ConfigurationException
2) 调用其超级构造函数:
public AbstractFileConfiguration(File file) throws ConfigurationException
{
this();
// set the file and update the url, the base path and the file name
setFile(file);
// load the file
if (file.exists())
{
load(); // method which interest you
}
}
3) 调用 load()
:
public void load() throws ConfigurationException
{
if (sourceURL != null)
{
load(sourceURL);
}
else
{
load(getFileName());
}
}
4) 调用 load(String fileName)
:
public void load(String fileName) throws ConfigurationException
{
try
{
URL url = ConfigurationUtils.locate(this.fileSystem, basePath, fileName);
if (url == null)
{
throw new ConfigurationException("Cannot locate configuration source " + fileName);
}
load(url);
}
catch (ConfigurationException e)
{
throw e;
}
catch (Exception e)
{
throw new ConfigurationException("Unable to load the configuration file " + fileName, e);
}
}
5) 调用 load(URL url)
public void load(URL url) throws ConfigurationException
{
if (sourceURL == null)
{
if (StringUtils.isEmpty(getBasePath()))
{
// ensure that we have a valid base path
setBasePath(url.toString());
}
sourceURL = url;
}
InputStream in = null;
try
{
in = fileSystem.getInputStream(url);
load(in);
}
catch (ConfigurationException e)
{
throw e;
}
catch (Exception e)
{
throw new ConfigurationException("Unable to load the configuration from the URL " + url, e);
}
finally
{
// close the input stream
try
{
if (in != null)
{
in.close();
}
}
catch (IOException e)
{
getLogger().warn("Could not close input stream", e);
}
}
}
并且在finally
语句中,可以看到inputstream在任何情况下都是关闭的。
PropertiesConfiguration.java
没有 close()
方法。是否需要做其他事情来释放文件?在生产中使用它之前,我想确定一下。我查看了代码,但没有看到任何内容。我不完全确定 PropertiesConfiguration.setProperty()
在不打开与随后必须关闭的文件的连接的情况下如何工作。
在 org.apache.commons.configuration.PropertiesConfiguration
中,当在 PropertiesConfiguration
实例中加载属性时,输入(流、路径、url 等...)当然会关闭。
你可以在org.apache.commons.configuration.AbstractFileConfiguration
的void load(URL url)
方法中得到确认。
调用此方法的方式如下:
1) PropertiesConfiguration
构造函数被调用:
public PropertiesConfiguration(File file) throws ConfigurationException
2) 调用其超级构造函数:
public AbstractFileConfiguration(File file) throws ConfigurationException
{
this();
// set the file and update the url, the base path and the file name
setFile(file);
// load the file
if (file.exists())
{
load(); // method which interest you
}
}
3) 调用 load()
:
public void load() throws ConfigurationException
{
if (sourceURL != null)
{
load(sourceURL);
}
else
{
load(getFileName());
}
}
4) 调用 load(String fileName)
:
public void load(String fileName) throws ConfigurationException
{
try
{
URL url = ConfigurationUtils.locate(this.fileSystem, basePath, fileName);
if (url == null)
{
throw new ConfigurationException("Cannot locate configuration source " + fileName);
}
load(url);
}
catch (ConfigurationException e)
{
throw e;
}
catch (Exception e)
{
throw new ConfigurationException("Unable to load the configuration file " + fileName, e);
}
}
5) 调用 load(URL url)
public void load(URL url) throws ConfigurationException
{
if (sourceURL == null)
{
if (StringUtils.isEmpty(getBasePath()))
{
// ensure that we have a valid base path
setBasePath(url.toString());
}
sourceURL = url;
}
InputStream in = null;
try
{
in = fileSystem.getInputStream(url);
load(in);
}
catch (ConfigurationException e)
{
throw e;
}
catch (Exception e)
{
throw new ConfigurationException("Unable to load the configuration from the URL " + url, e);
}
finally
{
// close the input stream
try
{
if (in != null)
{
in.close();
}
}
catch (IOException e)
{
getLogger().warn("Could not close input stream", e);
}
}
}
并且在finally
语句中,可以看到inputstream在任何情况下都是关闭的。