0

Vaadin ClassResource クラスを使用して、WAR でデプロイされるファイルからデータベース接続プロパティをロードしようとしていますが、ファイルが見つからないようです。私は Vaadin 6.5.2、Tomcat 7.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フォルダーの上部、現在の場所(メインアプリケーションと同じパッケージ内)など、さまざまな場所に構成ファイルを配置しようとしましたが、結果は常に同じ。誰が私が間違っているのか教えてもらえますか???

4

2 に答える 2

1

これは私たちのやり方です。

        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);

ただし、公平を期すために、戦争を引き起こしたり、アプリケーションを爆発させたりすることはありません。これはVaadinアプリケーションでApplication.classはなく、Vaadinアプリケーションの独自のラッパーであることに注意してください。

于 2011-03-08T09:03:12.743 に答える
0

.properties ファイルをアプリケーション コンテキスト ディレクトリにコピーし、次の方法で読み取りました

Properties properties = new Properties();
properties.load(new FileInputStream(getContext().getBaseDirectory().getAbsolutePath() + "/application.properties"));

アプリケーション クラスから上記のコードが呼び出されます。

于 2012-12-12T13:03:12.793 に答える