Announcement Announcement Module
Collapse
No announcement yet.
Simple AOP question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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).

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment


            • #7
              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.

              Comment

              Working...
              X