I have managed to get Introductions to work, in that an introduced concrete method does run.
I've included that as well as the failing advice so you can see that most of my cofig and setup must be correct... I wish!!
What is the thing I'm doing wrong?
To add to that - I can only get the following code to work for Introductions when I do not declare @Aspect in the class I want advising (MyWorld). Which sounds right.
But I can only get myPointcut declaration to be resolved if I put @Aspect back in. i.e.
@AfterReturning("timeout.helloworld.AnotherAspect. myPointcut()")
Does not raise an error, otherwise without the @Aspect I get:
error at ::0 can't find referenced pointcut myPointcut
There isn't this problem when I declare the Pointcut in place.
But anyway at no point am I able to see the Advice run during any combination of stuff I've tried. ie I never see output of:
""Here is my advice"
There seems so few worked examples out there to look at.![]()
I'd be very grateful if some kind soul could put me on track.![]()
HTML Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <aop:aspectj-autoproxy /> <bean id="anotherAspect" class="timeout.helloworld.AnotherAspect"></bean> <bean id="helloAspect" class="timeout.helloworld.HelloAspect"></bean> <bean id="myWorld" class="timeout.helloworld.worlds.MyWorld"></bean> </beans>Code:package timeout.helloworld; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.DeclareParents; @Aspect public class HelloAspect { @DeclareParents(value="timeout.helloworld.worlds.*+", defaultImpl=timeout.helloworld.DefaultWorld.class) public static World mixin; /* * Execute the following Advice after any method returns */ //@AfterReturning("timeout.helloworld.AnotherAspect.myPointcut()") @AfterReturning("execution (* *(..))") public void myAdvice() { System.out.println("Here is my advice"); } public void testAdvice() { System.out.println("HelloAspect.testAdvice()"); } }Code:package timeout.helloworld.worlds; import org.aspectj.lang.annotation.Aspect; //@Aspect // put it in take it out shake it all about :-( public class MyWorld { public String doSomut() { return "My World Doing something!!!!!!!!!"; } } package timeout.helloworld; public class DefaultWorld implements World { public String doSomut() { return "DefaultWorld does something!"; } } package timeout.helloworld; public interface World { String doSomut(); }Code:package timeout.helloworld; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.FileSystemXmlApplicationContext; @Aspect public class HelloRunner { public static void main(String[] args) { BeanFactory factory = new FileSystemXmlApplicationContext( "build/WEB-INF/classes/applicationContext/test-aspects.xml"); World world = (World) factory.getBean("myWorld"); System.out.println(world.doSomut()); AnotherAspect obj2 = (AnotherAspect) factory.getBean("anotherAspect"); obj2.testAdvice(); HelloAspect obj3 = (HelloAspect) factory.getBean("helloAspect"); obj3.testAdvice(); new HelloRunner().testAdvice(); } public void testAdvice() { System.out.println("HelloRunner.testAdvice()"); } }


Reply With Quote
