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

Thread: Metod Advice + inner object method invokes Question

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    13

    Default Metod Advice + inner object method invokes Question

    Hi Alls,

    I have 3 methods in my class
    Code:
    public void startRuleSet() {
        System.out.println("Start Rule Set");
        makeRule1();
        makeRule2();
      }
    
      public void makeRule1() {
        System.out.println("Rule 1");
      }
    
      public void makeRule2() {
        System.out.println("Rule 2");
      }
    in spring config there're such lines:
    Code:
      <bean id="myBefore" class="com.my.test.aop.MyBefore"/>
    
      <bean id="ruleSetAOPTest" class="com.my.test.aop.RuleSetAOPImpl"/>
    
      <bean id="ruleAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice">
          <ref local="myBefore"/>
        </property>
        <property name="pattern">
          <value>.*Rule.*</value>
        </property>
      </bean>
    
      <bean id="ruleSetBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
          <ref local="ruleSetAOPTest"/>
        </property>
        <property name="proxyTargetClass">
          <value>true</value>
        </property>
        <property name="interceptorNames">
          <list>
            <value>ruleAdvisor</value>
          </list>
        </property>
      </bean>
    And when i call startRuleSet
    Code:
    final RuleSetAOPImpl ruleSet = &#40;RuleSetAOPImpl&#41; context.getContext&#40;&#41;.getBean&#40;"ruleSetBean"&#41;;
        ruleSet.startRuleSet&#40;&#41;;
    I have advice invoke only on startRuleSet(), but i want that advice is invoked on makeRule* methods which are invoked inside startRuleSet method.

    Is it possible ?

    Thank's

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

  3. #3
    Join Date
    Mar 2005
    Posts
    13

    Default

    But in this case object has to know about AOP and that it is interceptored now.
    I don't like such solution because i need clean pojo not agly hybrid.

    Quote Originally Posted by irbouho

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    As I said in a previous post, this approach is intrusive. You may, however use AspectJ for less intrusive approach.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Mar 2005
    Posts
    13

    Default

    I cann't do it because my class instanses will load from db.
    And i think it's impossible to use AspectJ in such case only runtime byte code manipulation.
    Quote Originally Posted by irbouho
    As I said in a previous post, this approach is intrusive. You may, however use AspectJ for less intrusive approach.

  6. #6

    Default

    Quote Originally Posted by irbouho
    As I said in a previous post, this approach is intrusive. You may, however use AspectJ for less intrusive approach.
    Hello,

    What about this :

    I think this problem could be "partially" solved with the use of CLIB instead of Dynamic Proxies : since the target class will be subclassed, calls to this() will first cause the overriden method to execute, thus allowing the AOP mechanism to work.

    Of course there will still be some problems here with CGLib, since all the methods marked as "final" cannot be subclassed

    Do I have the point ? Or did I miss something ?

  7. #7
    Join Date
    Mar 2005
    Posts
    13

    Default

    You're right. I made this thing using cglib

    Quote Originally Posted by Laurent PETIT
    Quote Originally Posted by irbouho
    As I said in a previous post, this approach is intrusive. You may, however use AspectJ for less intrusive approach.
    Hello,

    What about this :

    I think this problem could be "partially" solved with the use of CLIB instead of Dynamic Proxies : since the target class will be subclassed, calls to this() will first cause the overriden method to execute, thus allowing the AOP mechanism to work.

    Of course there will still be some problems here with CGLib, since all the methods marked as "final" cannot be subclassed

    Do I have the point ? Or did I miss something ?

  8. #8
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    Do I have the point ? Or did I miss something?
    You missed something.

    Using CGLIB proxies will not solve this problem as, just like JDK proxies, they delegate to the target. See: http://forum.springframework.org/showthread.php?t=9926

    Ollie
    Last edited by robyn; May 16th, 2006 at 03:51 AM.

  9. #9
    Join Date
    Mar 2005
    Posts
    13

    Default

    You're wrong
    when i have 3 public methods:
    Code:
    public a() {
      b();
      c();
    }
    
    public b() {...}
    
    public c() {...}
    then using cglib proxy i can intercept for all 3 methods. And "a" method is invoked from outside but "b" and "c" from "a" and everything's working.

    Quote Originally Posted by oliverhutchison
    Do I have the point ? Or did I miss something?
    You missed something.

    Using CGLIB proxies will not solve this problem as, just like JDK proxies, they delegate to the target. See: http://forum.springframework.org/showthread.php?t=9926

    Ollie
    [/code]
    Last edited by robyn; May 16th, 2006 at 03:50 AM.

  10. #10
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    I would say the difference between Java proxies and CGLIB proxies is the following:

    A Java proxy effectively establishes a delegate. You call a method on the proxy which eventually delegates to the target. Once inside the target's method the invocation of another method will take place in the context of the target, not of the proxy.

    I assume a CGLIB proxy establishes a subclass of the target class. So you never leave the context. Operating on "this" effectively operates on the subclass (proxy) instance. So subsequent invocations take place on the proxied methods (if not private).

    It's a difference between delegation and inheritance, therefore the former does not work while the latter does.

    Regards,
    Andreas

    P.S.: I'm no expert for CGLIB. It's just an explanation of how I think it works

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  5. Replies: 8
    Last Post: Dec 7th, 2004, 06:13 PM

Posting Permissions

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