Results 1 to 7 of 7

Thread: Simple AOP question

  1. #1
    Join Date
    Aug 2006
    Posts
    16

    Default Simple AOP question

    I am seasoned Java programmer but new to AOP. After trying it with Spring, I have a question: Do I have to define interface for every class which I want to apply aspect? And does aspect only apply to public method?

    If it is true, consider following scenario:
    I have an aspect for Logging. Naturally, I want to apply it to a lot of classes. First, i want to log from a class without an interface. Second, it's not uncommon that user want to log from heavy-computing, but not public method.

    Is there any good way to deal with this?

    Thanks for the help!

  2. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by marmot101
    I am seasoned Java programmer but new to AOP. After trying it with Spring, I have a question: Do I have to define interface for every class which I want to apply aspect?
    In a lot of cases it is better to have an interface anyway.

    To answer you question:
    if you use Spring AOP (that is proxy based) you need an interface. If you use AspectJ you don't need an interface because the code of the aspect is added to the code of the classes (bytecode is modified).

  3. #3
    Join Date
    Aug 2006
    Posts
    16

    Default

    Thanks for the reply.

    I agree in a lot of cases, interface is good. But, if AOP in Spring means I have to define interface for every class that I want to use aspect, that's a overkill and unbearable burden.

    AspectJ sounds more reasonable.

  4. #4
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by marmot101
    Thanks for the reply.

    I agree in a lot of cases, interface is good. But, if AOP in Spring means I have to define interface for every class that I want to use aspect, that's a overkill and unbearable burden.
    I can understand your opinion, but desiging from interfaces made my life a lot easier. Testing for example: if you don't have interfaces, it is difficult to mock objects. And I also think it gives a good seperation between what is intended and how it is implemented.

    AspectJ sounds more reasonable.
    AspectJ is supported by Spring, check the documentation.

  5. #5
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Actually, you don't need to proxy an interface with Spring AOP - see the docs for ProxyFactoryBean and section 6.5.4 of the Spring reference guide. There is one technical limitation - the classes to be intercepted cannot have final methods. In practice this is hardly ever restrictive unless you are intercepting code that you do not own. The other limitation is that your intercepted object has to be a Spring bean (naturally).

    Also, I would warmly recommend AspectJ anyway for cases where you need to intercept objects that are not defined in a BeanFactory, or a large number of objects, or where you want to change the point cuts often. For a logging interceptor, if you want to intercept all classes, then AspectJ is the only way to go.

  6. #6

    Default

    If you specify an interface on the proxyfactorybean at uses standard jdk dynamic proxies. if you leave it out cglib is used which uses bytecode manipulation to construct a subclass of the class you are proxying on the fly. So no you do not need an interface for every class.

  7. #7
    Join Date
    Aug 2006
    Posts
    16

    Default

    It's great to see so many replies with fresh ideas! Thanks. I will play with those features and see which one fit my situation better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •