Article:
  An Introduction to Aspect-Oriented Programming with the Spring Framework, Part 1
Subject:   Automatic Swap of Target Class
Date:   2006-03-29 22:54:31
From:   spidercat
Hi


I have a question on how to swap the class? At the moment I have an existing class ClassA.foo, and I want this to use a different class instead ClassB to do a different implementation of the call (I am not using interfaces). I am not very familiar with AspectJ, but this concept is working of AspectJ, that is, it automatically swaps the class with pattern similar to the ClassA name and packaging. However for Spring, it seems like I couldnt find a way to automatically allow the his. I still have to use an interface first to specify in the context xml the advisors, and I have to use the application context xml.

Full Threads Oldest First

Showing messages 1 through 4 of 4.

  • Russell Miles photo Automatic Swap of Target Class
    2006-03-30 00:53:13  Russell Miles | O'Reilly AuthorO'Reilly Blogger [View]

    To my knowledge, in Spring you'd be best advised to do this sort of implementation replacement behind an interface anyway, and then to use regular AOP to change the actual implementation class. This is the approach advocated by the framework and is a best practice anyway, particularly for what you are attempting to do.
    • Automatic Swap of Target Class
      2006-03-30 19:04:31  spidercat [View]

      Hi Russ
      I am attempting to replace ClassA target to ClassB. So the pattern that I am trying to use is (if the package is com.xx), pointcut on com.xx.Class*, anything with Class on their name within the package will be replaced with ClassB. And ClassA is not an interface.

      I have used advises via methods,also using interfaces in spring, but for class, I only have seen it in AspectJ.

      It seems like jointpoints in spring framework is just limited to methods, and not classes yet. Here is the excerpt from the spring :
      "Joinpoint: Point during the execution of a program, such as a method invocation or a particular exception being thrown. In Spring AOP, a joinpoint is always method invocation. Spring does not use the term joinpoint prominently; joinpoint information is accessible through methods on the MethodInvocation argument passed to interceptors, and is evaluated by implementations of the org.springframework.aop.Pointcut interface."

      At the moment, I am still looking at the AspectJ document, coz I might just overlooked this.

      Thanks for the reply
      • Russell Miles photo Automatic Swap of Target Class
        2006-03-31 00:06:31  Russell Miles | O'Reilly AuthorO'Reilly Blogger [View]

        It might be worht having a look at the second article in this series (available at http://www.onjava.com/pub/a/onjava/2004/10/20/springaop2.html) and how I implemented the cuckoo's egg pattern? This pattern deals explicitly with the changing of an application's feature based on your business needs and so does some of what you are looking for
        • Automatic Swap of Target Class
          2006-04-06 01:57:53  spidercat [View]

          Hi Russ,

          Here is a better example of my question. If you look at the code using aspectJ, it is supposed to replace call at HelloWorld.say* method. An unlike what I've tried in Spring, HelloWord does not necessarily need an interface. Would you have any idea how this is done in Spring?

          // HelloWorld.java
          public class HelloWorld {
          public static void say(String message) {
          System.out.println(message);
          }

          public static void sayToPerson(String message, String name) {
          System.out.println(name + ", " + message);
          }
          }



          public aspect MannersAspect {
          pointcut callSayMessage() : call(public static void HelloWorld.say*(..));
          before() : callSayMessage() {
          System.out.println("Good day!");
          }

          after() : callSayMessage() {
          System.out.println("Thank you!");
          }
          }

          Cheers,

          Cathy