2009-09-04 15 views
2

私はSpringでwebAppを持っていますが、サーバモードでHSQLDBを使うと正しく動作しますが、ファイルモードではユニットテストに合格します。SpringでWebアプリケーションにHSQLDBを埋め込む方法

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 

    <property name="url" value="jdbc:hsqldb:hsql://localhost/images" /> 
    <property name="username" value="sa" /> 
    <property name="password" value="" /> 
</bean> 

は、私はちょうどこの

 <property name="url" value="jdbc:hsqldb:file:data/images" />  
    (-- In file) 

のために、このライン

<property name="url" value="jdbc:hsqldb:hsql://localhost/images" />  
    (-- Server mode) 

を変更し、それだけでユニットテストを通過して、ウェブapppに失敗します。これは私のデータソースです。

ファイルモードでwebappを実行すると、HSQLDBは​​データベースのファイルを見つけられません。

私は既にwebappのルートとweb-infの内部にデータベースのデータを入れようとしましたが、うまくいきません。

答えて

0

絶対パスを使用する必要があります。 Webアプリケーションを実行すると、現在の(または現在の)ディレクトリは、アプリケーションサーバーのbinフォルダになる可能性が最も高いです。したがって、HSQLDB URLの相対パスを指定すると、webappのルートからの相対パスではなく、そこからこのファイルをロードしようとします。

1

私はここで答えを見つけました:

http://coding.derkeiler.com/Archive/Java/comp.lang.java.databases/2003-11/0096.html

を、私は私のプロジェクトpersistenceLayerWithData.jar上のジャーに内部デ・データを置きます。 、

<property name="url" value="jdbc:hsqldb:res:/data/images" /> 

そして私はlibにWEB-INF /にjarファイルを追加し、(達人でこのすべてを)戦争をするとTomcatに配備し、それが動作します:

は、それから私は、データソースにこのpropetyを設定します私はwebsphere 7にも展開しています。

しかし、私はwhit mvn jettyを実行します。実行しないと、jetty whit mavenを実行しているときにclaspathにjarファイルを置かないと思います。まあ

+0

データベースファイルはsrc/main/resourcesにありますか?また、このデータベースを編集する予定ですか? –

2

あなたはこのようにjarファイル内のデータベースを置く場合:読み取り専用としてあなたが失敗した場合、データベースを挿入または変更しようと

<property name="url" value="jdbc:hsqldb:res:/data/images" /> 

あなたが唯一、それを使用することができます。

解決策の1つは、リスナーをweb.xmlに配置することです。アプリケーションが起動すると、アプリケーションWebのルートパスで初期化されます。

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name>maf</display-name> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 


    <!--This is to get root of the aplication--> 

    <listener> 
     <listener-class> 
    org.atoms.HsqlDatabaseListener 
     </listener-class> 
    </listener> 


     <!--Este listener se encarga de inicializar todo el contenedor de Spring y mantener una variable en el 
    ServletContext que apunta a dicho contenedor --> 

    <listener> 
     <listener-class> 
    org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 




</web-app> 

リスナー:

package org.atoms; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 


/** 
* 
* @author atomsfat 
*/ 
public class HsqlDatabaseListener implements ServletContextListener { 

    private ServletContext context = null; 

    public void contextInitialized(ServletContextEvent event) { 
     context = event.getServletContext(); 

     String prefix = event.getServletContext().getRealPath("/"); 

     System.out.println("database root " + prefix); 
     com.atoms.HsqlDatabasePathResolver.getInstance(prefix); 


    } 

    public void contextDestroyed(ServletContextEvent event) { 
     context = event.getServletContext(); 

    } 

他のクラス:

package com.atoms; 


/** 
* 
* @author atomsfat 
*/ 
public class HsqlDatabasePathResolver { 


    private static HsqlDatabasePathResolver instance ; 
    private static String applicationPath = ""; 

    private HsqlDatabasePathResolver() { 
    } 

     /** Get Instance. 
    */ 
    static public synchronized HsqlDatabasePathResolver getInstance(String applicationPath) { 

     if (instance == null) { 

      HsqlDatabasePathResolver.applicationPath = 
        HsqlDatabasePathResolver.normalizePath(applicationPath); 
      instance = new HsqlDatabasePathResolver(); 



      System.out.println("Inizalizando path : " + HsqlDatabasePathResolver.applicationPath); 

     } 
     return instance; 
    } 

    public String getApplicationPath() { 
     return applicationPath; 
    } 



     public String getUrlDatabase(String urlDatabase) { 


     return HsqlDatabasePathResolver.replaceAll(urlDatabase,"{apppath}", applicationPath); 
    } 


     /** 

     * 
     * replace the "\" character by "/" and remove relative paths 
     * 
     * @param path 
     * @return 
     */ 
     public static String normalizePath(String path) { 
      if (path == null) { 
       return null; 
      } 
      String normalized = path; 
      if (normalized.equals("/.")) { 
       return "/"; 
      } 
      if (normalized.indexOf('\\') >= 0) { 
       normalized = normalized.replace('\\', '/'); 
      } 
      if (!normalized.startsWith("/") && normalized.indexOf(':') < 0) { 
       normalized = "/" + normalized; 
      } 
      do { 
       int index = normalized.indexOf("//"); 
       if (index < 0) { 
        break; 
       } 
       normalized = normalized.substring(0, index) + normalized.substring(index + 1); 
      } while (true); 
      do { 
       int index = normalized.indexOf("/./"); 
       if (index < 0) { 
        break; 
       } 
       normalized = normalized.substring(0, index) + normalized.substring(index + 2); 
      } while (true); 
      do { 
       int index = normalized.indexOf("/../"); 
       if (index >= 0) { 
        if (index == 0) { 
         return null; 
        } 
        int index2 = normalized.lastIndexOf('/', index - 1); 
        normalized = normalized.substring(0, index2) + normalized.substring(index + 3); 
       } else { 
        return normalized; 
       } 
      } while (true); 
     } 




     public static String replaceAll(String str, String match, String replace) { 
      if (match == null || match.length() == 0) { 
       return str; 
      } 
      if (replace == null) { 
       replace = ""; 
      } 
      if(match.equals(replace))return str; 
      StringBuffer ret=new StringBuffer(); 
      int i = str.indexOf(match); 
      int y = 0; 
      while (i >= 0) 
      { 
       //System.out.println("i:"+i+" y:"+y); 
       ret.append(str.substring(y, i)); 
       ret.append(replace); 
       //str = str.substring(y, i) + replace + str.substring(i + match.length()); 
       y = i + match.length(); 
       i = str.indexOf(match,y); 
      } 
      ret.append(str.substring(y)); 
      return ret.toString(); 
     } 



} 

そして、この私が春に使用した構成:

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-2.5.xsd 
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> 

    <!-- La definición del Datasource --> 
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true" 
     destroy-method="close"> 
      <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 

      <property name="url"> 
       <ref bean="dataBaseUrl"/> 
      </property> 

      <property name="username" value="sa" /> 
      <property name="password" value="" /> 
     </bean> 

    <!-- La definición del Factory de Session con Anotaciones --> 
     <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init="false"> 
      <property name="dataSource" ref="dataSource" /> 
      <property name="hibernateProperties"> 
       <props> 
        <prop key="hibernate.dialect"> 
     org.hibernate.dialect.HSQLDialect 
        </prop> 
        <prop key="hibernate.show_sql">true</prop> 
        <prop key="hibernate.format_sql">true</prop> 
       </props> 
      </property> 
      <property name="mappingResources"> 
       <list> 
        <value>Atoms.hbm.xml</value> 
       </list> 

      </property> 
     </bean> 
    <!--HibernaTemplate--> 
     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
      <property name="sessionFactory"> 
       <ref bean="sessionFactory" /> 
      </property> 
     </bean> 
    <!-- Definición de los DAO`s --> 

     <bean id="ipBlancaDao" class="org.atoms.impl.AtomsDaoHibernateImpl"> 
      <property name="hibernateTemplate" ref="hibernateTemplate" /> 
     </bean> 


     <!--If your are not running in Web this will initialize with the directory from the process was started note that this classes is a singleton so if you are running in web the listener already have initialize the class with the path of the class--> 


     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> 


    <bean id="hsqlDatabasePathResolver" class="com.atoms.HsqlDatabasePathResolver" factory-method="getInstance" lazy-init="false"> 
      <constructor-arg> 
       <value>${user.dir}</value> 
      </constructor-arg> 
     </bean> 

    <!--This bean just replace {apppath} whit the absolute path--> 

     <bean id="dataBaseUrl" class="java.lang.String" factory-bean="hsqlDatabasePathResolver" lazy-init="false" 
         factory-method="getUrlDatabase"> 
      <constructor-arg> 
       <value>jdbc:hsqldb:mem:{apppath}/WEB-INF/data/maf</value> 
      </constructor-arg> 

     </bean> 
    </beans> 

そして、はい、この作品が、混乱です、私は解決策があると思いますリスナーを使用すると、appWebのパスを取得できます。誰かがこれを簡単にすることができれば、答えを投稿してください。

関連する問題