從Tapestry4.0開始使用了Hivemind作為注入容器,合理有效的使用該容器,可以極大的簡化開發工作,減少代碼量.比如說,在開發Hibernate應用時,就有一個關于事務控制的問題,在Spring中可以使用Spring來管理事務,但是我覺得那樣對自己的代碼侵入性比較大,而利用Hivemind中的攔截器,則可以方便的解決此問題,而且侵入性小.
步驟是這樣的:1.配置Hibernate Session服務,并保證該服務是線程級的.2.配置攔截器服務,并向其注入Hibernate Session服務.3.配置DAO服務,并配置其攔截器為上一步的攔截器.
具體的實現如下:1.配置文件
<?xml?version="1.0"?>
<!--?
???Copyright?2004,?2005?The?Apache?Software?Foundation

???Licensed?under?the?Apache?License,?Version?2.0?(the?"License");
???you?may?not?use?this?file?except?in?compliance?with?the?License.
???You?may?obtain?a?copy?of?the?License?at

???????http://www.apache.org/licenses/LICENSE-2.0

???Unless?required?by?applicable?law?or?agreed?to?in?writing,?software
???distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS,
???WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied.
???See?the?License?for?the?specific?language?governing?permissions?and
???limitations?under?the?License.
-->

<module?id="mymodule"?version="4.0.0">

<service-point?id="userDao"?interface="com.ims.dao.UserInfoDAO">
????<invoke-factory>
????????<construct?class="com.ims.dao.impl.UserinfoDAOImpl">
????????????<set-object?property="session"?value="service:hibernateSession"/>
????????</construct>
????????
????</invoke-factory>
????<interceptor?service-id="ims.SessionInterceptor">
????????<include?method="make*"/>
????</interceptor>
</service-point>
<service-point?id="SessionInterceptor"?interface="org.apache.hivemind.ServiceInterceptorFactory">
????<invoke-factory>?
????<construct?class="com.hivemind.util.interceptor.SessionInterceptor"?>?
????????<set-object?property="session"?value="service:hibernateSession"?/>?
????</construct>?
</invoke-factory>?

</service-point>


<service-point?id="hibernateHivemindFactory"?interface="org.apache.hivemind.ServiceImplementationFactory">
????<invoke-factory>
????????<construct?class="com.hivemind.util.HibernateHivemindFactory"?initialize-method="init">
????????????<set-object?property="threadEventNotifier"?value="service:hivemind.ThreadEventNotifier"/>
????????</construct>
????</invoke-factory>
</service-point>
<service-point?id="hibernateSession"?interface="org.hibernate.Session">
????<invoke-factory?service-id="ims.hibernateHivemindFactory"?model="threaded">
????<config?file="hibernate.cfg.xml"/>
????</invoke-factory>
</service-point>
</module>
2.代碼:

/**?*//**
?*?SessionInterceptor.java
?*
?*??*/
package?com.hivemind.util.interceptor;

import?java.lang.reflect.InvocationHandler;
import?java.lang.reflect.InvocationTargetException;
import?java.lang.reflect.Method;
import?java.lang.reflect.Proxy;
import?java.util.List;

import?org.apache.commons.logging.Log;
import?org.apache.hivemind.Element;
import?org.apache.hivemind.InterceptorStack;
import?org.apache.hivemind.ServiceInterceptorFactory;
import?org.apache.hivemind.internal.Module;
import?org.apache.hivemind.service.impl.LoggingUtils;
import?org.apache.hivemind.util.StringUtils;
import?org.hibernate.Session;
import?org.hibernate.Transaction;


/**?*//**
?*?Hibernate?Session的攔截器,對符合指定條件的方法進行攔截,?加入事務處理功能。
?*?
?*??
?*/

public?class?SessionInterceptor?implements?ServiceInterceptorFactory?
{
????private?String[]?required_method;

????private?Session?session;


????public?void?setSession(Session?session)?
{
????????this.session?=?session;
????}


????/**?*//**
?????*?核心服務方法,生成攔截器
?????*/
????public?void?createInterceptor(InterceptorStack?stack,

????????????Module?invokingModule,?List?parameters)?
{
????????Log?log?=?stack.getServiceLog();

????????for?(int?i?=?0;?i?<?parameters.size();?i++)?
{
????????????Object?obj?=?parameters.get(i);

????????????if?(obj?instanceof?Element)?
{
????????????????Element?element?=?(Element)?obj;

????????????????if?("include".equals(element.getElementName()))?
{
????????????????????String?value?=?element.getAttributeValue("method");
????????????????????required_method?=?StringUtils.split(value);
????????????????}

????????????}
????????}
????????Object?obj?=?stack.peek();

????????InvocationHandler?handler?=?new?HibernateSessionInvocationHandler(log,
????????????????obj,?session,?required_method);
????????Object?interceptor?=?Proxy.newProxyInstance(invokingModule

????????????????.getClassResolver().getClassLoader(),?new?Class[]?
{?stack
????????????????.getServiceInterface()?},?handler);
????????stack.push(interceptor);
????}

}


/**?*//**
?*?攔截器方法調用處理類。
?*?
?*/

class?HibernateSessionInvocationHandler?implements?InvocationHandler?
{
????private?Log?log;


????/**?*//**
?????*?原始對象
?????*/
????private?Object?delegate;

????private?Session?session;


????/**?*//**
?????*?配置的方法名稱列表
?????*/
????private?String[]?requredName;

????public?HibernateSessionInvocationHandler(Log?log,?Object?delegate,

????????????Session?session,?String[]?required_name)?
{
????????this.log?=?log;
????????this.delegate?=?delegate;
????????this.session?=?session;
????????this.requredName?=?required_name;

????}


????/**?*//**
?????*?處理攔截到的方法
?????*/
????public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)

????????????throws?Throwable?
{
????????Object?result?=?null;
????????boolean?debug?=?log.isDebugEnabled();
????????Transaction?tx?=?null;


????????if?(debug)?
{
????????????LoggingUtils.entry(log,?method.getName(),?args);
????????}


????????if?(isMatch(method.getName(),?requredName))?
{

????????????try?
{
????????????????tx?=?session.beginTransaction();
????????????????result?=?method.invoke(delegate,?args);


????????????????if?(debug)?
{
????????????????????if?(method.getReturnType()?==?void.class)
????????????????????????LoggingUtils.voidExit(log,?method.getName());
????????????????????else
????????????????????????LoggingUtils.exit(log,?method.getName(),?result);
????????????????}
????????????????tx.commit();
????????????????return?result;

????????????}?catch?(InvocationTargetException?ex)?
{

????????????????try?
{
????????????????????tx.rollback();

????????????????}?catch?(Exception?e)?
{

????????????????}
????????????????Throwable?targetException?=?ex.getTargetException();

????????????????if?(debug)
????????????????????LoggingUtils.exception(log,?method.getName(),
????????????????????????????targetException);

????????????????throw?targetException;

????????????}

????????}?else?
{

????????????try?
{
????????????????result?=?method.invoke(delegate,?args);

????????????????if?(debug)?
{
????????????????????if?(method.getReturnType()?==?void.class)
????????????????????????LoggingUtils.voidExit(log,?method.getName());
????????????????????else
????????????????????????LoggingUtils.exit(log,?method.getName(),?result);
????????????????}
????????????????return?result;

????????????}?catch?(InvocationTargetException?e)?
{
????????????????Throwable?targetException?=?e.getTargetException();

????????????????if?(debug)
????????????????????LoggingUtils.exception(log,?method.getName(),
????????????????????????????targetException);

????????????????throw?targetException;
????????????}

????????}

????}


????/**?*//**
?????*?當前調用的方法名稱是否符合指定的條件
?????*?
?????*?@param?name
?????*?@param?names
?????*?@return
?????*/

????private?boolean?isMatch(String?name,?String[]?names)?
{
????????if?(names?==?null)
????????????return?false;

????????for?(int?i?=?0;?i?<?names.length;?i++)?
{
????????????String?sname?=?names[i];

????????????if?(name.equals(sname)?||?sname.endsWith("*")
????????????????????&&?name.startsWith(sname.substring(0,?sname.length()?-?1))
????????????????????||?sname.startsWith("*")
????????????????????&&?name.endsWith(sname.substring(1)))
????????????????return?true;

????????}
????????return?false;
????}
}

步驟是這樣的:1.配置Hibernate Session服務,并保證該服務是線程級的.2.配置攔截器服務,并向其注入Hibernate Session服務.3.配置DAO服務,并配置其攔截器為上一步的攔截器.
具體的實現如下:1.配置文件





















































2.代碼:











































































































































































































































