代码之家  ›  专栏  ›  技术社区  ›  les2

在没有Spring或AOP的情况下,如何处理JAX-WS中的横切conerns?处理者?

  •  0
  • les2  · 技术社区  · 17 年前

    不过,我确实有更具体的想法:

    @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管理的bean:(

    当做

    3 回复  |  直到 16 年前
        1
  •  1
  •   James Black    17 年前

    AspectJ是Spring推出后的一个选项吗?

    或者,您可以使用反射,然后重新设计应用程序来处理这个概念吗?

    有关反思的回顾,请参阅本文: http://onjava.com/pub/a/onjava/2007/03/15/reflections-on-java-reflection.html

    或者重新设计您的类以使用抽象类,这样performFoo将位于抽象类中,所以您不需要进行复制和粘贴。在你的例子中,你几乎做到了。

        2
  •  0
  •   les2    16 年前

    JAX-WS Commons Spring Extention 使我的web服务impl成为一个spring管理的bean,并使用around advice在一个地方处理所有锅炉板。

    如果我想保持no 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);
            }
        });
    }
    

        3
  •  0
  •   alves    16 年前

    最好的方法是使用处理程序,但您必须使用 @HandlerChain 注释:

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

    档案 将定义您的处理程序:

    <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>
    

    public class LoggingHandler implements SOAPHandler<SOAPMessageContext> {
    
       // this is called before invoking the operation and after invoking the operation
       public boolean handleMessage(SOAPMessageContext ctx) {
          log(ctx);
          return true;
       }
    
       public boolean handleFault(SOAPMessageContext ctx) {
          log(ctx);
          return true;
       }
    }
    

    更多细节 here .

    推荐文章