Announcement Announcement Module
Collapse
No announcement yet.
Difference between @target and @within in Spring AOP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Difference between @target and @within in Spring AOP

    Hi

    I was just wondering, if there is a difference and if yes where the difference is between @target(annotation) and @within(annotation) when using Spring AOP.

    From the Spring reference manual:

    any join point (method execution only in Spring AOP) where the target object has an @Transactional annotation:
    @target(org.springframework.transaction.annotation .Transactional)

    any join point (method execution only in Spring AOP) where the declared type of the target object has an @Transactional annotation:
    @within(org.springframework.transaction.annotation .Transactional)

  • #2
    Difference is in retention policy requirements

    This is a good question. One difference between the two is that @within() is matched statically, requiring the corresponding annotation type to have only the CLASS retention. Whereas, @target() is matched at runtime, requiring the same to have the RUNTIME retention. Other than that, within the context of Spring, there is no difference between the join points selected by two.

    In AspectJ, there would be very significant differences (dynamic vs. static selection and join points selection in nested classes) between the two.

    -Ramnivas

    Comment


    • #3
      Hi Ramnivas

      Thanks for your explanation, just prepared a small test project (attached with this post), helping me to understand the small but important difference.

      Thanks again..

      Comment


      • #4
        And what's then the difference between target and within?

        Comment


        • #5
          Here are the differences:

          1. target() can be used to collect the target object (by specifying an identifier instead of a type). within(), on the other hand, can be used only to perform selection.
          2. target() specifies the specific target type, whereas within() specifies the lexical scope for the type (so it can be an outer class, for example).
          3. target() must specify a type (and not a type pattern). within(), however, may specify either.

          -Ramnivas

          Comment


          • #6
            @target proxies everything?

            So this seems to explain something I'm seeing. When I turn on aspectj-autoproxy, and I have an aspect that uses an "@target()" pointcut, it appears that every bean in my context gets proxied. Is this correct? Is this what Spring needs to do to detect the annotation at runtime?

            I see this because I just added such an aspect to my project and stuff is breaking all over the place, because some folks have used specific types rather than interfaces for the types of properties. Now the objects are being aop-proxied and the setters are no longer working (the proxies have the interface type, but the setters were coded specifically).

            Obviously, the setters should be typed with interfaces, but I also don't want every single bean in my app context to have a proxy generated for it. Is there some way around this? Or must I abandon "@target"?

            Comment


            • #7
              Apparently that is the case.

              When I changed my pointcut from a simple "@target(annotation)" to include the Controller.handleRequest method signature (as in this post), now only the controllers are proxied and not every single bean in the context. Whew!

              Comment

              Working...
              X