Friday, October 7, 2011

GWT Spring Hibernate

How to wiring GWT with Spring and Hibernate
 Web.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
        'http://java.sun.com/dtd/web-app_2_3.dtd'>
<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">

    <servlet>
        <servlet-name>controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/controller-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>controller</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

      <welcome-file-list>
        <welcome-file>Main.html</welcome-file>
    </welcome-file-list>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
  
</web-app>


projectname .gwt.xml


<?xml version="1.0" encoding="UTF-8"?>
<module>

    <!-- Inherit the core Web Toolkit stuff.                  -->
    <inherits name="com.google.gwt.user.User"/>
    <inherits name="com.google.gwt.user.ClippedImage"/>
    <inherits name="com.gwtext.GwtExt"/>
    <inherits name="com.google.gwt.core.Core"/>
    <inherits name="com.google.gwt.user.History"/>
    <inherits name="com.google.gwt.i18n.I18N"/>
    <inherits name="com.gwtext.Pagebus"/>
    <inherits name="com.gwtext.SyntaxHighlighter"/>
    <inherits name="com.gwtextux.GwtExtUx"/>
    <inherits name="com.google.gwt.user.ClippedImage"/>
    <stylesheet src="themes/green/css/xtheme-green.css"/>
    <stylesheet src="themes/slate/css/xtheme-slate.css"/>
    <stylesheet src="themes/indigo/css/xtheme-indigo.css"/>
    <stylesheet src="themes/silverCherry/css/xtheme-silverCherry.css"/>

    <!-- Specify the app entry point class.   -->
    <entry-point class="com.gwtproject.client.Main"/>
    <!-- RPC service servlet declarations          -->
    <servlet class="com.gwtproject.server.service.TestServiceImpl"
             path="/testService.do"/>
   
    <!-- uncomment below "user.agent" and "locale" properties to speedup gwt compiler
    (do only in development environment)

    For Firefox: <set-property name="user.agent" value="gecko1_8" />
    For IE: <set-property name="user.agent" value="ie6" />
    For Both: <set-property name="user.agent" value="ie6,gecko1_8" />

    <extend-property name="locale" values="en_UK" />
    -->

    <!--<set-property name="user.agent" value="ie6"/>-->
    <extend-property name="locale" values="en_UK"/>


</module>



controller-servlet.xml


<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <!-- The application context definition for the DispatcherServlet -->

    <!-- Maps the request through to a concrete controller instance -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <map>
                <entry key="/testService.do" value-ref="testServiceController"/>
           </map>
        </property>
        <property name="interceptors">
            <list>
                <ref bean="openSessionInViewInterceptor"/>
            </list>
        </property>
    </bean>


    <!-- Function End -->

     <bean id="testServiceController"
          class="com.projectname.server.service.TestServiceImpl">
        <property name="testDao" ref="testDaoImpl"/>
    </bean>

    <bean id="testDaoImpl" class="com.projectname.server.dao.TestDaoImpl">
        <property name="sessionFactory" ref="factory"/>
    </bean>


    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>
    <!--  Multipart form data handler -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="10000000"/>
    </bean>


    <bean name="openSessionInViewInterceptor"
          class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory" ref="factory"/>
    </bean>


    <import resource="classpath:applicationContext.xml"/>

</beans>



applicationContext.xml


<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


    <import resource="classpath:spring-dao-config.xml"/>

    <bean id="testDaoImpl" class="com.projectname.server.dao.impl.TestDaoImpl"
          scope="prototype">
        <property name="sessionFactory">
            <ref bean="factory"/>
        </property>
    </bean>

</beans>


spring-dao-config.xml


<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>WEB-INF/db.properties</value>
        </property>
    </bean>


    <!--Data base  configured to c3po pool.in this case we are using postgres and mssql server databses-->
    <bean id="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass"><value>${db.driver_class}</value></property>
        <property name="jdbcUrl"><value>${db.url}</value></property>
        <property name="user"><value>${db.username}</value></property>
        <property name="password"><value>${db.password}</value></property>
    </bean>


    <!-- FactoryBean that creates a Hibernate SessionFactory.
         This is the usual way to set up a shared Hibernate SessionFactory in a Spring application context;
         the SessionFactory can then be passed to Hibernate-based DAOs via dependency injection.
     -->
    <bean id="factory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
             <!-- configure your hbm.xml file here-->
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.default_schema">${db.default_schema}</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
                <prop key="hibernate.c3p0.minPoolSize">20</prop>
                <prop key="hibernate.c3p0.maxPoolSize">100</prop>
                <prop key="hibernate.c3p0.timeout">1800</prop>
                <prop key="hibernate.c3p0.max_statement">0</prop>
                <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
                <prop key="hibernate.c3p0.acquire_increment">5</prop>
                <prop key="hibernate.c3p0.maxIdleTime">5</prop>
                <!-- enable second level cash using EHcash bundle -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>

            </props>
        </property>


        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="factory"/>
        </property>

        <property name="nestedTransactionAllowed" value="true"/>
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <!--
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="false"/>
     -->
    <!--Enable annotation base transaction and
        explicitly directing  to exsisting TRN manager -->
    <tx:annotation-driven transaction-manager="txManager"/>

</beans>



do you have any issues about this please send email to my email address (saneera@gmail.com)

No comments:

Post a Comment