Announcement Announcement Module
Collapse
No announcement yet.
No matching factory method found: factory method 'aspectOf' Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No matching factory method found: factory method 'aspectOf'

    I have a simple AOP test project, that works fine with runtime dynamic proxies. However using compile-time waving, i get a runtime exception.

    1/ The files

    The test:
    Code:
    package hellotrackworld;
    
    import javax.annotation.Resource;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration ( locations={"/resources/application-context.xml"} )
    public class TestAOP
    {
    	@Resource ( name="fooService" ) 
    	private FooService fooService;//The intercepted service
    
    	@Test
    	public void toto ()
    	{
    		fooService.foo();
    	}
    }
    The intercepted service:
    Code:
    package hellotrackworld;
    
    public interface FooService 
    {
    	void foo ();
    }
    And its implementation:
    Code:
    package hellotrackworld;
    
    import org.springframework.stereotype.Service;
    
    @Service("fooService")
    public class FooServiceImpl implements FooService 
    {
    	@Override
    	public void foo() 
    	{
    		System.out.println ( "foo" );
    	}
    }
    The intercepting aspect:
    Code:
    package hellotrackworld;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class AspectTata
    {
    	@Before ( "anyPublicOperation()" )
    	public void printTata ()
    	{
    		System.out.println ( "!!!!!TATA!!!!!!" );
    	}
    	
    	@SuppressWarnings("unused")
    	@Pointcut("execution(public * *(..))")
        private void anyPublicOperation() {}
    }
    And finally the spring config:
    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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        
        <context:component-scan base-package="hellotrackworld"/>
        <context:annotation-config/>
        
        
        <!--<bean class="hellotrackworld.AspectTata"/>
        <aop:aspectj-autoproxy/>-->
        
        <bean name="aspectTata" class="hellotrackworld.AspectTata" factory-method="aspectOf"/>
        
    </beans>

  • #2
    2/The output

    When using runtime weaving (<aop:aspectj-autoproxy/>):
    Code:
    [org.springframework.test.context.TestContextManager][main] - @TestExecutionListeners is not present for class [class hellotrackworld.TestAOP]: using defaults.
    [org.springframework.beans.factory.xml.XmlBeanDefinitionReader][main] - Loading XML bean definitions from class path resource [resources/application-context.xml]
    [org.springframework.context.support.GenericApplicationContext][main] - Refreshing [email protected]6632: display name [[email protected]6632]; startup date [Tue Jul 21 11:28:07 CEST 2009]; root of context hierarchy
    [org.springframework.context.support.GenericApplicationContext][main] - Bean factory for application context [[email protected]6632]: org.springframework.beans.factory.support.DefaultListableBeanFactory@6b9c84
    [org.springframework.beans.factory.support.DefaultListableBeanFactory][main] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6b9c84: defining beans [fooService,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,hellotrackworld.AspectTata#0,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
    !!!!!TATA!!!!!!
    foo
    [org.springframework.context.support.GenericApplicationContext][Thread-1] - Closing [email protected]6632: display name [[email protected]6632]; startup date [Tue Jul 21 11:28:07 CEST 2009]; root of context hierarchy
    [org.springframework.beans.factory.support.DefaultListableBeanFactory][Thread-1] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6b9c84: defining beans [fooService,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,hellotrackworld.AspectTata#0,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
    -->The aspect is called, everything is fine

    When using compile-time weaving (factory-method="aspectOf"):

    The build log:
    Code:
    Buildfile: D:\Documents and Settings\Ext-ClaisseL\workspace\Probleme AOP\build.xml
    compile:
         [iajc] weaveinfo Join point 'method-execution(void hellotrackworld.FooServiceImpl.foo())' in Type 'hellotrackworld.FooServiceImpl' (FooServiceImpl.java:9) advised by before advice from 'hellotrackworld.AspectTata' (AspectTata.java:11)
         [iajc] weaveinfo Join point 'method-execution(void hellotrackworld.TestAOP.toto())' in Type 'hellotrackworld.TestAOP' (TestAOP.java:21) advised by before advice from 'hellotrackworld.AspectTata' (AspectTata.java:11)
    BUILD SUCCESSFUL
    Total time: 2 seconds
    -->Looks correct to me

    The log output running the test:
    Code:
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:203)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:93)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:130)
    	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aspectTata' defined in class path resource [resources/application-context.xml]: No matching factory method found: factory method 'aspectOf'
    	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:417)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:903)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:817)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:199)
    	... 16 more

    Comment


    • #3
      If you use compile time weaving you don't need the aspectOf... The aspect is already compiled into your class...

      The 'aspectOf' only works with aspects written in the AspectJ language not annotations (afaik) and is needed to define something as an aspect so it can be used with loadtime weaving.

      Comment


      • #4
        Yes, i had forgotten about that. Thanks.
        So my aspect should be already woven into my beans (the iajc log seems to indicate it went right), but when i run the test the aspect isn't called:
        Code:
        [org.springframework.test.context.TestContextManager][main] - @TestExecutionListeners is not present for class [class hellotrackworld.TestAOP]: using defaults.
        [org.springframework.beans.factory.xml.XmlBeanDefinitionReader][main] - Loading XML bean definitions from class path resource [resources/application-context.xml]
        [org.springframework.context.support.GenericApplicationContext][main] - Refreshing [email protected]190: display name [[email protected]190]; startup date [Tue Jul 21 13:48:55 CEST 2009]; root of context hierarchy
        [org.springframework.context.support.GenericApplicationContext][main] - Bean factory for application context [[email protected]190]: org.springframework.beans.factory.support.DefaultListableBeanFactory@7bc899
        [org.springframework.beans.factory.support.DefaultListableBeanFactory][main] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7bc899: defining beans [fooService,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
        foo
        [org.springframework.context.support.GenericApplicationContext][Thread-0] - Closing [email protected]190: display name [[email protected]190]; startup date [Tue Jul 21 13:48:55 CEST 2009]; root of context hierarchy
        [org.springframework.beans.factory.support.DefaultListableBeanFactory][Thread-0] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7bc899: defining beans [fooService,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
        -->No "TATA" log

        Comment


        • #5
          You should only need the FooService defined or use component scanning. So if you use compile time weaving make sure there is nothing else in there. It should even work if you simple create a new instance of the bean.

          With compile time weaving you actually modify your class files and the aspects become part of the class file. If it idoesn't work, your classes aren't woven or you are using the wrong (older) classes..

          Comment


          • #6
            The aspect is correctly woven into the classes, if i specify a destDir to the iajc task.

            before weaving:
            Code:
            public void foo();
                0  getstatic java.lang.System.out : java.io.PrintStream [17]
                3  ldc <String "foo"> [23]
                5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]
                8  return
                  Line numbers:
                    [pc: 0, line: 11]
                    [pc: 8, line: 12]
                  Local variable table:
                    [pc: 0, pc: 9] local: this index: 0 type: hellotrackworld.FooServiceImpl
            after weaving:
            Code:
             public void foo();
                 0  invokestatic hellotrackworld.AspectTata.aspectOf() : hellotrackworld.AspectTata [42]
                 3  invokevirtual hellotrackworld.AspectTata.printTata() : void [45]
                 6  getstatic java.lang.System.out : java.io.PrintStream [18]
                 9  ldc <String "foo"> [24]
                11  invokevirtual java.io.PrintStream.println(java.lang.String) : void [25]
                14  return
                  Line numbers:
                    [pc: 0, line: 11]
                    [pc: 14, line: 12]
                  Local variable table:
                    [pc: 6, pc: 15] local: this index: 0 type: hellotrackworld.FooServiceImpl
            Now i should setup the IDE to use this bin-aj folder with the instrumented code instead. Any idea how to do it? (even if it's not strictly a Spring question)
            -->I edited the test's runtime, now the aspect is called but it is called twice..

            Edit:
            Nevermind, it was normal. I had forgotten that with compile-time weaving, non-Spring bean invocations would also match my anyPublicOperation() pointcut. I have adapted my aspect and now it works:
            Code:
            package hellotrackworld;
            import org.aspectj.lang.annotation.Aspect;
            import org.aspectj.lang.annotation.Before;
            import org.aspectj.lang.annotation.Pointcut;
            
            @Aspect
            public class AspectTata
            {
            	@Before ( "executionFoo()" )
            	public void printTata ()
            	{
            		System.out.println ( "!!!!!TATA!!!!!!" );
            	}
            	
            	@SuppressWarnings("unused")
            	@Pointcut("execution(public * *(..))")
                private void anyPublicOperation() {}
            	
            	@SuppressWarnings("unused")
            	@Pointcut("@annotation ( com.thalesgroup.JACOMO.components.impl.aspects.Transactional )")
                private void transactional() {}
            	
            	@SuppressWarnings("unused")
            	@Pointcut("execution(public * hellotrackworld.FooService.foo(..))")
                private void executionFoo() {}
            }
            Last edited by chtimi; Jul 21st, 2009, 09:57 AM.

            Comment


            • #7
              Pls Help Me With Code

              Originally posted by Marten Deinum View Post
              You should only need the FooService defined or use component scanning. So if you use compile time weaving make sure there is nothing else in there. It should even work if you simple create a new instance of the bean.

              With compile time weaving you actually modify your class files and the aspects become part of the class file. If it idoesn't work, your classes aren't woven or you are using the wrong (older) classes..
              Hi Marten,

              The code which had given in the pdf is not working. Could you please give me the code which is working fine for AOP schema based.

              Thanks in advance,
              _Sukku.

              Comment

              Working...
              X