Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Hooking into Spring weaving

  1. #1
    Join Date
    Apr 2008
    Location
    Lomma, Sweden
    Posts
    41

    Default Hooking into Spring weaving

    I am wondering if it is possible to hook into springs weaving instead of creating another proxy when using AOP?

    Here is a simple example:

    Code:
    (at)Transactional
    (at)Lockable 
    public void save() {}
    By default, this will create two proxies. Spring will create one for (at)Transactional, and my BeanPostProcessor will create another one for
    my custom (at)Lockable interface.

    Is it somehow possible to "hook" into the weaving process of spring and add my custom Advice to the same ProxyFactory that is used for the (at)Transactional annotation?

    If not, is there any "best practices" for minimizing the number of proxies created?

    I do NOT want to use aspectJ expressions.

    Kind regards

    /johan

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    The best practice is the one you don't want to use.

    I suggest you check the code for how spring does it. You can simply check the type of the bean if it already is a proxy (i.e. object implementing Advised) then you can simply add the interceptor/advice adding your behavior.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2008
    Location
    Lomma, Sweden
    Posts
    41

    Default

    Aha, well if the proxy implements Advised it could work. However, I see a few problems...
    1. Ordering of advises will be difficult
    2. Is it possible to access any annotations set on the class, not the interface?

  4. #4
    Join Date
    Apr 2008
    Location
    Lomma, Sweden
    Posts
    41

    Default

    This will of course not be a problem since I have access to the target.
    Quote Originally Posted by johras View Post
    2. Is it possible to access any annotations set on the class, not the interface?
    The reason I want to try this out since I want type-safety and pure java refactoring to work for my pointcuts.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If that is the reason not to use AspectJ.... With AspectJ you can have all that also...

    Also if your Advice implements Ordered there isn't an issue either, also the ordering of your Bean(Factory)PostProcessor is taken into account.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Apr 2008
    Location
    Lomma, Sweden
    Posts
    41

    Default

    You are right... this is really good. Seems lite everything is in place then.

    <tx:annotation-driven order="1"/>
    And my custom advice to a higher or lower order depending on what I want.

    However, if aspectj expressions are best practice and I get them to be typesafe and refactoring friendly that sounds good but could you please point me in the right direction here.

    For me, "@annotation(MyCustomAnnotation)" is neither? Even if it is much better than using scary execution pointcuts...

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Why is the execution pointcut scary?! You can use your own annotation for matching (I.e. the @Lockable)...

    Code:
    <aop:pointcut expression="execution(* (@Lockable *..*.*(..)))" />
    You can even bind it to a variable if you want.

    Code:
    public Object aroundLockable(ProceedingJoinPoint pjp,Object target, Lockable lock) {
      // do your stuff.
      Object retVal = pjp.proceed();
      return retVal;
    }
    Code:
    <bean id="myAspect" class="myAspectClass"/>
    
    <aop:config>
    <aop:pointcut id="lockableMethod" expression="execution(* (@Lockable *..*.*(..)) and target(target) and annotation(lock)" />
    <aop:aspect ref="myAspect">
      <aop:around pointcut-ref="lockableMethod" method="aroundLockable" />
    </aop:aspect>
    </aop:config>
    Ofcourse you can do the same with @Aspect or the AspectJ language instead of in XML.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Apr 2008
    Location
    Lomma, Sweden
    Posts
    41

    Default

    I meant examples like "execution (* *..services.*.save(..)" are scary since
    since the expression is a string and it will not be changed if I change my method to
    saveAndUpdate(). The same goes for changing an annotation name regardless if I am using @annoation or execution, changing the name of the annotation does not change my pointcuts.

    Using springs AnnotationMatchingPointcut however solves this. I can also easily find my pointcuts by following references to the annotation in my IDE. I feel more confident that my pointcuts will always work since I should get a compiler error if the annotation does not match and I do not have to rely so heavily on tests to prove that my advices are executed when expected.

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Again with proper tooling your statement isn't true. If you use eclipse and the AJDT. Then you will get all that if you use @Aspects or aspectj languague. And it gives you more control and easy of use then messing around with all the pre aspectj stuff..

    Also if you write your pointcut correct (and you should at least write 1 test case!) and you messup your test fails...
    Last edited by Marten Deinum; Oct 7th, 2008 at 01:43 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  10. #10
    Join Date
    Apr 2008
    Location
    Lomma, Sweden
    Posts
    41

    Default

    Thanks, your input is very appreciated!

    I agree that it would be much easier to use @Aspect annotation on my advice and in our project it could probably work especially since we are using a minimal number of pointcuts. A also agree that it is a bit "messy" to solve it the way I want it and it may not be the best possible solution in the long run.

    BUT... perhaps I get get more control and easy of use using aspectJ expressions, I would still feel more confident with:
    Code:
    @Aspect
    public void MyClass {
        @AnnotationMatchingPointcut(MyCustomAdvice.class)
        public void method() {}
    over:
    Code:
    @Aspect
    public void MyClass {
        @Pointcut("@annotation(MyCustomAdvice")
        public void method() {}
    Kind regards

    /johan

Posting Permissions

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