如果没有 Spring 或 AOP,如何处理 JAX-WS 中的横切问题?处理程序?

发布于 2024-08-05 09:24:01 字数 738 浏览 13 评论 0原文

然而,我确实有一些更具体的想法:

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

帮助!!!!! 提供建议!请不要让我复制粘贴该样板 10 亿次(正如我被指示做的那样) )。

问候, 莱斯

I do have something more specific in mind, however:

Each web service method needs to be wrapped with some boiler place code (cross cutting concern, yes, spring AOP would work great here but it either doesn't work or unapproved by gov't architecture group). A simple service call is as follows:

@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(...);

(I hope that's enough context). Basically, I would like a hook (that was in the same thread - like a method invocation interceptor) that could have a before() and after() that could could soil(something) and wash(something) around the method call for every freaking WebMethod.

Can't use Spring AOP because my web services are not Spring managed beans :(

HELP!!!!! Give advice! Please don't let me copy-paste that boiler plate 1 billion times (as I've been instructed to do).

Regards,
LES

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青柠芒果 2024-08-12 09:24:02

Spring 退出后,AspectJ 是一个选择吗?

或者,您可以使用反射,然后重新设计您的应用程序以使用此概念吗?

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

或者重新设计你的类以使用抽象类,所以performFoo将在抽象类中,所以你不需要这样做复制并粘贴。在你的例子中你已经差不多了。

Is AspectJ an option since Spring is out?

Or, can you use Reflection, and just redesign your application to work with this concept?

For a review on reflection you can look at this article:
http://onjava.com/pub/a/onjava/2007/03/15/reflections-on-java-reflection.html

Or redesign your class to use an abstract class, so performFoo would be in the abstract class, so you don't do copy and paste. You are almost there in your example.

筱武穆 2024-08-12 09:24:02

我最终使用了 JAX-WS Commons Spring Extension 并制作了我的网站service impl 是一个 spring 管理的 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 方法创建的上下文中执行的代码。我确信这种模式有一个名称(让我想起贷款模式)。

I ended up using the JAX-WS Commons Spring Extention and made my web service impl a spring managed bean and used around advice to handle all that boiler plate in one place.

If I wanted to maintain the original constraint of no AOP, I suppose I could have created an interface and a helper method as follows:

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

public T doOperation(Operation<T> op) {

    // before advice

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

Finally, the business methods would be coded as follows:

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

Basically, each business method would use the doOperation helper method in it's body and an anonymous class containing the code that needs to execute within the context created by the doOperation method. I'm sure there's a name for this pattern (reminds me of the Loan Pattern).

终陌 2024-08-12 09:24:02

最好的方法是使用处理程序,但您必须使用 @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 类:

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

更多详细信息此处

The best way would be to use Handlers, but you would have to annotate all your services with the @HandlerChain annotation:

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

The file handlers.xml would define your handlers:

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

Finally, implement your Handler class like this:

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

More details here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文