Announcement Announcement Module
Collapse
No announcement yet.
Simple AOP app with aop schema doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple AOP app with aop schema doesn't work

    I can not understand what I'm doing wrong. Granted I'm new to AOP, but have been using Spring for about a year.

    Here's my pojo that I want intercepted:
    Code:
    package no.test.aop;
    
    public class HelloThereBean {
    
    	public String sayHalloThere(){
    		return "Hello there";
    	}
    	
    }
    The interceptor that should print "HelloInterceptor" befor the pojo is called:
    Code:
    package no.test.aop;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    public class HelloInterceptor implements MethodInterceptor {
    
    	public Object invoke(MethodInvocation invocation) throws Throwable {
    		System.out.println("HelloInterceptor");		
    		return invocation.proceed();
    	}
    }
    The test code:
    Code:
    package no.test.aop;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestHelloThere {
    	
    	ApplicationContext ctx;
    	
    	@Before
    	public void setUp(){
    		ctx = new ClassPathXmlApplicationContext("no/test/aop/applicationContext.xml");
    	}
    	
    	@Test
    	public void testHelloIsIntercepted(){
    		HelloThereBean helloThere = (HelloThereBean) ctx.getBean("helloThereBean");
    		helloThere.sayHalloThere();
    	}
    }
    Finally, the all important 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:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    	<bean id="helloThereBean" class="no.test.aop.HelloThereBean" />
    	
    	<bean id="helloInterceptor" class="no.test.aop.HelloInterceptor"/>
    	
    	<aop:config>
    	   <aop:aspect ref="alterHello">	
    	   
    	      <aop:pointcut id="helloThereExecution"
    	                 expression="execution(* no.test.aop.*(..))"
    					/>
    	
    	      <aop:around pointcut-ref="helloThereExecution"
    	               method="invoke"/>
    	
    	   </aop:aspect>
    	</aop:config>
    	
    
    </beans>
    The AOP config has pretty much been copied from the reference documentation. (6.3.7)

    The error message I get:
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAd visor': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJAroundAdvic e] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.config.MethodLocatingFacto ryBean] while setting constructor argument with index 0; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'alterHello' is defined
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.config.MethodLocatingFacto ryBean] while setting constructor argument with index 0; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'alterHello' is defined
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'alterHello' is defined
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'alterHello' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:356)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:916)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getType(AbstractBeanFactory.java:477)
    at org.springframework.aop.config.MethodLocatingFacto ryBean.setBeanFactory(MethodLocatingFactoryBean.ja va:68)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1162)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:425)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBean(BeanDefinitio nValueResolver.java:215)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:127)
    at org.springframework.beans.factory.support.Construc torResolver.resolveConstructorArguments(Constructo rResolver.java:373)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:120)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:799)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:717)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:384)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBean(BeanDefinitio nValueResolver.java:215)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:127)
    at org.springframework.beans.factory.support.Construc torResolver.resolveConstructorArguments(Constructo rResolver.java:389)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:120)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:799)
    at ........
    Any help I can get would be appreciated. I'm sure it's all my fault, and that I'm being stupid. Still, I can't figure it out ;-)

  • #2
    Originally posted by ramin_farhanian
    take a look at the spring reference, the aop section. You are using AspectJ without having it configured in your project.
    ramin.
    Where is the assumption that I'm using aspectj made? Can't find anything in the reference doc about configuring my project for aspectj?

    I see that there is an alternativ to use Spring AOP instead. Not sure what the difference is, and what ties my code/config to aspectj? Is it the fact that I'm using MethodInterceptor interface that forces me to use aspectj?

    Comment


    • #3
      I've altered my configuration slightly, it know looks like this:

      Code:
      	<bean id="helloThereBean" class="no.test.aop.HelloThereBean" />
      	
      	<bean id="helloInterceptor" class="no.test.aop.HelloInterceptor"/>
      
      	<aop:config>
      	   <aop:aspect ref="helloInterceptor">	
      	   
      	      <aop:pointcut id="helloThereExecution"
      	                 expression="execution(* no.test.aop..*.*(..))"
      					/>
      	
      	      <aop:around pointcut-ref="helloThereExecution"
      	               method="invoke"/>
      	
      	   </aop:aspect>
      	</aop:config>
      Now I got a new exorg.springframework.beans.factory.BeanCreationEx ception:
      Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAd visor': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdv isor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
      Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdv isor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
      Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
      at org.aspectj.weaver.tools.PointcutParser.parsePoint cutExpression(PointcutParser.java:315)
      at org.springframework.aop.aspectj.AspectJExpressionP ointcut.buildPointcutExpression(AspectJExpressionP ointcut.java:189)
      at org.springframework.aop.aspectj.AspectJExpressionP ointcut.checkReadyToMatch(AspectJExpressionPointcu t.java:176)
      at org.springframework.aop.aspectj.AspectJExpressionP ointcut.getMethodMatcher(AspectJExpressionPointcut .java:162)
      at org.springframework.aop.aspectj.AbstractAspectJAdv ice.buildSafePointcut(AbstractAspectJAdvice.java:1 81)
      at org.springframework.aop.aspectj.AspectJPointcutAdv isor.<init>(AspectJPointcutAdvisor.java:51)
      at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Construc tor.java:494)
      at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:85)
      ception:

      Comment


      • #4
        You are mixing some aop usage cases there. You want to use an aop:around tag with MethodInterceptor. Have a read to aop section in Spring reference documentation.
        Try like this:
        Code:
        public class HelloInterceptor {
        
        	public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
        		System.out.println("start HelloInterceptor");
        		Object result = pjp.proceed();
        		System.out.println("end HelloInterceptor");
        		return result;
        	}
        }

        Comment


        • #5
          It works!

          I kind of figured out what I was doing wrong. With the following code, everything works fine:
          Code:
          	<bean id="helloThereBean" class="no.test.aop.HelloThereBean" />
          	
          	<bean id="helloInterceptor" class="no.test.aop.HelloInterceptor"/>
          
          	<aop:config>
          	      <aop:pointcut id="helloMethods" expression="execution(* no.test.aop..*.*(..))"/>
          	      <aop:advisor advice-ref="helloInterceptor" pointcut-ref="helloMethods" />
          	
          	</aop:config>
          Thanks for all the feedback

          Comment

          Working...
          X