2009-09-23 51 views
0

但是,我确实有一些更具体的内容:如何在不使用Spring或AOP的情况下处理JAX-WS中的横切裁剪?处理程序?

每个Web服务方法都需要用一些锅炉位代码包装(交叉切割问题,是的,Spring AOP在这里很好用,但它不起作用或未经政府建筑组织批准)。一个简单的服务电话如下:

@WebMethod... 
public Foo performFoo(...) { 

    Object result = null; 
    Object something = blah; 
    try { 
     soil(something); 

     result = handlePerformFoo(...); 
    } catch(Exception e) { 
     throw translateException(e); 
    } finally { 
     wash(something); 
    } 
    return result; 
} 

protected abstract Foo handlePerformFoo(...); 

(我希望这是足够的上下文)。基本上,我想要一个钩子(它与方法调用拦截器在同一个线程中)可能有一个before()和after(),可能会在方法调用的每个方法周围(某些事物)吓跑WebMethod。

无法使用Spring AOP的,因为我的web服务并不Spring管理豆:(

HELP !!!!!给意见!请不要让箱十亿次复制,粘贴锅炉板(因为我已经指示进行操作)。

问候, LES

回答

0

我结束了使用JAX-WS Commons Spring Extention,并使我的web服务impl春天管理bean和周围的建议来处理所有的锅炉板在一个地方。

如果我想保持不AOP的原始约束,我想我可能已经创建了一个接口和一个辅助方法如下:

interface Operation<T> { 
    T execute(); 
} 

public T doOperation(Operation<T> op) { 

    // before advice 

    try { 
     return op.execute(); 
    } catch(Throwable ex) { 
     // handle ... 
    } finally { 
     // clean up ... 
    } 
} 

最后,如下的业务方法将编码:

public Result computeResult(final String item, final int number) { 
    return doOperation(new Operation<Result>(){ 
     public Result execute() { 
      return new Result(item + ": processed", number * 5); 
     } 
    }); 
} 

基本上,每个业务方法将使用在它的身上doOperation辅助方法,以及包含需要由doOperation方法创建的上下文中执行代码的匿名类。我确信这个模式有一个名字(让我想起贷款模式)。

1

AspectJ是否因为春天的选项呢?

或者,您也可以使用反射,只是重新设计应用程序一起工作这个概念?

有关反射的评论,你可以看看这篇文章: http://onjava.com/pub/a/onjava/2007/03/15/reflections-on-java-reflection.html

或重新设计你的类使用抽象类,所以performFoo将是抽象类,所以你不要做复制和粘贴。在你的例子中,你几乎就在那里。

0

,最好的办法是使用处理程序,但你必须标注所有服务与@HandlerChain注释:

@WebService(name = "AddNumbers") 
@HandlerChain(file = "handlers.xml") // put handlers.xml in WEB-INF/classes 
public class AddNumbersImpl implements AddNumbers 
{ 
... 
} 

文件handlers.xml将定义您的处理程序:

<handler-chains xmlns="http://java.sun.com/xml/ns/javaee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee"> 
    <handler-chain> 
    <handler> 
     <handler-name>LoggingHandler</handler-name> 
     <handler-class>demo.handlers.common.LoggingHandler</handler-class> 
    </handler> 
    </handler-chain> 
</handler-chains> 
最后,像这样实现你的Handler类:

更多详细信息here

相关问题