Re: [aspectwerkz-user] Advisable.aw_addAdvice at a class definition level
Jonas Bonér <[email protected]> Wed, 23 Nov 2005 13:17:12 +0100
| Newsgroups | gmane.comp.java.aspectwerkz.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Derek. Proxy based AOP is for per-instance deployment of aspects only. E.g. you add aspects to a single instance. If you want to weave into the class and have it affect all instances of this class you should use the regular way of using AW. E.g.: - aop.xml - startup options for the VM etc. See f.e. this article (a bit outdated but not too much): http://docs.codehaus.org/display/AW/Hello+World Good luck. /Jonas On 11/23/05, [email protected] <[email protected]> wrote: > Hi All, > > I am new to Aspects and this may be a stupid question so apologies in advance if it is. > > I am using the Advisable interface to add some BeforeAdvice behaviour to a method call on an object I created with the Proxy.newInstance() method. This is well and good, but what I really want to do is to add the given BeforeAdvice to all instances of the class as I will have no control over its instantiation in reality. I need to use the Java API for this as these pointcuts will be specified on the fly. > > I have attached a small class below to demonstrate the problem a bit more effectively than via my words. > > Any help would be greatly appreciated. > > Derek > > public class Callback { > > public void step1() { > System.out.println(" - TraceMe1.step1"); > step2(); > } > > protected void step2() { > System.out.println(" - TraceMe1.step2"); > } > > public static void main(String[] args) { > > // get a proxy instance > System.out.println("\nINFO:: ------ get a weaved proxy to the Callback class -----"); > Callback callback = (Callback) Proxy.newInstance(Callback.class, false, true); > > // adding around interceptor using Advisble API > System.out.println("\nINFO:: ------ adding aspects on the fly -----"); > ((Advisable) callback).aw_addAdvice( > "execution(* *.step1())", > new BeforeAdvice() { > public void invoke(JoinPoint jp) throws Throwable { > Object target = jp.getTarget(); > System.out.println("about to call step1() in Target: " + target); > } > } > ); > > // invoking the method chain with the added around interceptor > System.out.println("**** callback.step1()"); > callback.step1(); > > // what do I need to do to get the BeforeAdvice to work on this object without creating it via the Proxy? > Callback callbackNonProxied = new Callback(); > System.out.println("**** callbackNonProxied.step1()"); > callbackNonProxied.step1(); > } > > Visit our website at http://www.ubs.com > > This message contains confidential information and is intended only > for the individual named. If you are not the named addressee you > should not disseminate, distribute or copy this e-mail. Please > notify the sender immediately by e-mail if you have received this > e-mail by mistake and delete this e-mail from your system. > > E-mail transmission cannot be guaranteed to be secure or error-free > as information could be intercepted, corrupted, lost, destroyed, > arrive late or incomplete, or contain viruses. The sender therefore > does not accept liability for any errors or omissions in the contents > of this message which arise as a result of e-mail transmission. If > verification is required please request a hard-copy version. This > message is provided for informational purposes and should not be > construed as a solicitation or offer to buy or sell any securities or > related financial instruments. > > -- Jonas Bonér Terracotta, Inc. http://www.terracottatech.com http://blogs.codehaus.org/people/jboner/