配置与启动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()) ;
相关文章
相关标签/搜索