我尝试使用Vaadin ClassResource类从将与WAR一起部署的文件加载数据库连接属性,但似乎找不到该文件。我使用的是Vaadin6.5.2,Tomcat7.0.6,并且我已经将我的"app.properties“文件放在与我的应用程序主文件相同的包中。
我的代码是用Scala编写的。下面是我的尝试:
val cr = new ClassResource("app.properties",this) // "this" is the application
debug("resource mimeType = {}",cr.getMIMEType)
debug("resource bufferSize = {}", cr.getBufferSize)
debug("resource cacheTime = {}",cr.getCacheTime)
debug("resource fileName = {}", cr.getFilename)
val ds = cr.getStream
if (ds != null) {
debug("download stream bufferSize = {}", ds.getBufferSize)
debug("download stream cacheTime = {}",ds.getCacheTime)
val is = ds.getStream // get InputStream
if (is != null) {
val props = new Properties
props.load(is)
val dbHost = props.get("db.host").asInstanceOf[String]
val dbName = props.get("db.name").asInstanceOf[String]
val dbPort = props.get("db.port").asInstanceOf[String]
val dbUser = props.get("db.user").asInstanceOf[String]
val dbPass = props.get("db.pass").asInstanceOf[String]
val dbUri = props.get("db.uri").asInstanceOf[String]
} else {
debug("Input stream was null")
}
} else {
debug("Download stream was null")
}结果如下:
08:51:59.617 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource mimeType = application/octet-stream
08:51:59.620 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource bufferSize = 0
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource cacheTime = 86400000
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource fileName = app.properties
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - download stream bufferSize = 0
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - download stream cacheTime = 86400000
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - Input stream was null我尝试过将配置文件放在不同的位置,包括src的顶部,包含主题的VAADIN文件夹的顶部,以及它当前所在的位置(与主应用程序在同一个包中),但结果总是相同的。谁能告诉我我哪里做错了?
发布于 2011-03-08 17:03:13
这就是我们怎么做的。
InputStream is=null;
try
{
is=Application.class.getClassLoader().getResourceAsStream("Application.properties");
}
catch(Exception x)
{
log.error("Error loading 'Application.properties' properties",x);
return null;
}
if (is!=null)
{
try
{
Properties props=new Properties();
props.load(is);
return(props);
}
catch (IOException e)
{
log.error("Error reading properties 'Application.properties' ",e);
}
}
return(null);然而,公平地说,我们不会制造战争,也不会让我们的应用程序爆炸。请记住,Application.class不是Vaadin应用程序,而是我们自己的Vaadin应用程序包装器。
发布于 2012-12-12 21:03:13
我将.properties文件复制到应用程序上下文目录中,并以以下方式读取它
Properties properties = new Properties();
properties.load(new FileInputStream(getContext().getBaseDirectory().getAbsolutePath() + "/application.properties"));从应用程序类调用上述代码。
https://stackoverflow.com/questions/5222632
复制相似问题