配置與啓動guzz

配置與啓動guzz

guzz程序的核心爲GuzzContext對象,完成GuzzContext的初始化並獲取其引用,便可使用guzz的所有功能。

Standalone程序:

建立guzz的核心配置文件guzz.xml,並存在classpath目錄下。
1 import org.guzz.Configuration;
2 import org.guzz.GuzzContext;
3 
4 GuzzContext gc = new Configuration("classpath:guzz.xml").newGuzzContext() ;
5 //perform you actions......
6 //.....
7 //shutting it down when you application exit.
8 gc.shutdown() ;

普通的Web應用:

建立guzz的核心配置文件guzz.xml,並存在在/WEB-INF/目錄下。
修改web.xml文件,增長以下項:
 1 <context-param>
 2    <param-name>guzzConfigLocation</param-name>
 3    <param-value>/WEB-INF/guzz.xml</param-value>
 4 </context-param>
 5 
 6 <listener>
 7    <listener-class>
 8       org.guzz.web.context.ContextLoaderListener
 9    </listener-class>
10 </listener>
此時在jsp頁面中就可使用guzz的taglib進行數據庫操做。

 

GuzzContext 將會在web app退出時,由容器通知關閉。

 

使用Spring IOC的web應用程序:

  • 1. 建立guzz的核心配置文件guzz.xml,並存在在/WEB-INF/目錄下。
  • 2. 修改web.xml的配置項,將spring的ContextLoader的Loader定義刪掉,如:
1 <listener>
2         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3 </listener>

換成Guzz針對spring的Listener:java

1 <listener>
2       <listener-class>org.guzz.web.context.spring.GuzzWithSpringContextLoaderListener</listener-class>
3 </listener>
  • 3. 修改spring的applicationContext.xml,增長GuzzContext的配置:
1 <bean id="guzzContext" class="org.guzz.web.context.spring.GuzzContextBeanFactory" factory-method="createGuzzContext">
2     <constructor-arg><value>/WEB-INF/guzz.xml</value></constructor-arg>
3 </bean>

通常狀況下,咱們還須要增長一個BaseDao的bean,相似hibernate中的sessionFactory.getHibernateTemplate(),基於此建立應用本身的Dao或Manager。web

1 <bean id="abstractGuzzDao" class="org.guzz.dao.GuzzBaseDao" abstract="true">
2      <property name="guzzContext" ref="guzzContext" />
3 </bean>
  • 4. 支持spring聲明式事務(須要guzz 1.3.0+):
修改guzz.xml,在tran定義上增長一個屬性:locator="spring" 相似這樣:
1 <tran locator="spring">

若是您在spring中配置過Hibernate的聲明式事務,把配置copy過來,而後(通常在applicationContext.xml文件中)修改transactionManager以下便可:spring

1 <bean id="transactionManager" class="org.guzz.web.context.spring.GuzzTransactionManager">  
2         <property name="guzzContext" ref="guzzContext" />
3 </bean>
若是你忘了怎麼配置,這是一篇很好的文章: Spring事務配置的五種方式

補丁: 若是是基於annotation @Transactiona的事務聲明,請下載 http://guzz.googlecode.com/svn/wiki/no-wikis/GuzzBaseDao.javaencoding:UTF-8 這個類,在本身的工程中建個org.guzz.dao而後放進去,覆蓋jar包中的類。數據庫

  • 5. 至此,就完成了guzz和spring IOC的集成。guzzContext能夠經過spring bean獲取的,也能夠經過GuzzWebApplicationContextUtil獲取到。

 

在web應用中獲取GuzzContext:

按照普通web或者spring web方式配置的guzz,能夠在servlet和JSP中,經過以下方式獲取到GuzzContext:session

1 import org.guzz.GuzzContext;
2 
3 //session is HttpSession
4 //or pass ServletContext
5 GuzzContext gc = org.guzz.web.context.GuzzWebApplicationContextUtil.getGuzzContext(session.getServletContext()) ;
相關文章
相關標籤/搜索