Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > AOP (Aspect Oriented Programming)

Reply
 
Thread Tools Display Modes
  #1  
Old Mar 17th, 2005, 05:16 PM
darkit darkit is offline
Junior Member
 
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 = (RuleSetAOPImpl) context.getContext().getBean("ruleSetBean");
    ruleSet.startRuleSet();
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
Reply With Quote
  #2  
Old Mar 17th, 2005, 08:11 PM
irbouho irbouho is offline
Senior Member
 
Join Date: Aug 2004
Location: Montréal, Canada
Posts: 848
Default

Please take a look at AOP, Proxies, and method calling in same instance and Pre-Interceptor in TransactionProxyFactory.
HTH
__________________
Omar Irbouh

Spring Modules Team
http://irbouh.blogspot.com/
Reply With Quote
  #3  
Old Mar 17th, 2005, 08:47 PM
darkit darkit is offline
Junior Member
 
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
Reply With Quote
  #4  
Old Mar 17th, 2005, 08:51 PM
irbouho irbouho is offline
Senior Member
 
Join Date: Aug 2004
Location: Montréal, Canada
Posts: 848
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/
Reply With Quote
  #5  
Old Mar 17th, 2005, 09:07 PM
darkit darkit is offline
Junior Member
 
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.
Reply With Quote
  #6  
Old Jun 22nd, 2005, 06:40 AM
Laurent PETIT Laurent PETIT is offline
Junior Member
 
Join Date: Oct 2004
Posts: 11
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 ?
Reply With Quote
  #7  
Old Jun 22nd, 2005, 07:44 AM
darkit darkit is offline
Junior Member
 
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 ?
Reply With Quote
  #8  
Old Jun 23rd, 2005, 07:21 PM
oliverhutchison oliverhutchison is offline
Senior Member
 
Join Date: Aug 2004
Location: Melbourne, Australia
Posts: 335
Default

Quote:
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.
Reply With Quote
  #9  
Old Jun 24th, 2005, 02:44 AM
darkit darkit is offline
Junior Member
 
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
Quote:
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.
Reply With Quote
  #10  
Old Jun 24th, 2005, 02:58 AM
Andreas Senft Andreas Senft is offline
Senior Member
 
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Order of Bean definitions matters? cfuser Core Container 2 Oct 21st, 2005 10:29 AM
Spring container fails with no exception naor Core Container 9 Oct 1st, 2005 03:39 PM
EHCaching Hibernate dencamel Data Access 3 Sep 6th, 2005 09:03 PM
Stack Overflow rayho222 Core Container 6 May 17th, 2005 03:42 AM
Method call why not be intercepted by MethodSecurityIntercep aaron8tang Spring Security 8 Dec 7th, 2004 06:13 PM


All times are GMT -5. The time now is 01:58 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.