Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Renamed
Wiki Markup
{scrollbar}

Service Advisors

Service advice represents a powerful meta-programming facility available to services. In fact, it is a kind of limited Aspect Oriented Programming.

...

Code Block
  @Match("*")
  public static void adviseNonNull(MethodAdviceReceiver receiver)
  {
    MethodAdvice advice = new MethodAdvice()
    {
      void advise(Invocation invocation)
      {
        invocation.proceed();

        if (invocation.getResult().equals(null))
          invocation.overrideResult("");
      }
    };

    for (Method m : receiver.getServiceInterface().getMethods())
    {
      if (m.getReturnType().equals(String.class))
        receiver.adviseMethod(m, advice);
    }
  };

Built-in Advice

Tapestry includes two built-in advisor services.

Logging Advice

Logging advice is built into Tapestry. You can apply logging advice to your services very easily:

...

LoggingAdvisor is a built-in Tapestry IoC service. This demonstrates how services can be injected into service advisor methods. The Logger parameter is the logger for the service being advised.

Lazy Advice

LazyAdvisor makes method invocations lazy: methods that return an interface (rather than a value) will not execute immediately; instead, the method invocation is postponed until a method of the return value is invoked.

Matching And Ordering

Each service advice method gets a unique id, obtained by stripping the "advise" prefix from the method name. Advice ids must be unique across all modules.

...

In many cases, the order in which the advice is given is very important; for example, you may want logging first, then transaction management, then security checks. The @Order annotation allows you to explicitly set the order.

Decorators and Advice

Service decorators are another way to achieve the same thing; service advisors are a more recent addition, added in Tapestry 5.1.

...