Announcement Announcement Module
Collapse
No announcement yet.
Bizarre error in AOP - @After cannot be an AspectJ annotation. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bizarre error in AOP - @After cannot be an AspectJ annotation.

    The ERROR
    Code:
    Dec 17, 2011 7:15:38 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13f0c45f: startup date [Sat Dec 17 19:15:38 IST 2011]; root of context hierarchy
    Dec 17, 2011 7:15:38 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [AOP.xml]
    Dec 17, 2011 7:15:39 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5054a835: defining beans [org.springframework.aop.config.internalAutoProxyCreator,businessLogic,aopAspect]; root of factory hierarchy
    Dec 17, 2011 7:15:39 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5054a835: defining beans [org.springframework.aop.config.internalAutoProxyCreator,businessLogic,aopAspect]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessLogic' defined in class path resource [AOP.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.After(value=aspectOperations()) cannot be an AspectJ annotation
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    	at ankeet.spring.tester.Tester.main(Tester.java:20)
    Caused by: java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.After(value=aspectOperations()) cannot be an AspectJ annotation
    	at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory$AspectJAnnotation.<init>(AbstractAspectJAdvisorFactory.java:250)
    	at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.findAnnotation(AbstractAspectJAdvisorFactory.java:88)
    	at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(AbstractAspectJAdvisorFactory.java:77)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getPointcut(ReflectiveAspectJAdvisorFactory.java:140)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisor(ReflectiveAspectJAdvisorFactory.java:130)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory$1.doWith(ReflectiveAspectJAdvisorFactory.java:74)
    	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:452)
    	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:430)
    	at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisors(ReflectiveAspectJAdvisorFactory.java:70)
    	at org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder.buildAspectJAdvisors(BeanFactoryAspectJAdvisorsBuilder.java:109)
    	at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:86)
    	at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:107)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:278)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:848)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:820)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:446)
    	... 10 more
    Caused by: java.lang.NoSuchMethodException: $Proxy4.argNames()
    	at java.lang.Class.getMethod(Class.java:1622)
    	at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory$AspectJAnnotation.<init>(AbstractAspectJAdvisorFactory.java:247)
    	... 25 more
    I'm just trying to run a simple aspect on calling of a method.
    The Method
    Code:
    package ankeet.spring.business;
    
    public class BusinessLogic {
    	
    	public void iDontKnowAboutAspects() {
    		System.out.println("Start of function.");
    		System.out.println("This is a Dummy function.");
    		System.out.println("End of function.");
    	}
    }
    The Aspect
    Code:
    /**
     * 
     */
    package ankeet.spring.aop;
    
    import java.util.Arrays;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    
    /**
     * @author Ankeet Maini
     *
     */
    @Aspect
    public class AOPAspect {
    	
    	@SuppressWarnings("unused")
    	@Pointcut("execution(* *.*(..))")
    	private void aspectOperations(){}
    	
    	@Before(value = "aspectOperations()")
    	public void before(JoinPoint joinPoint) {
    
    		System.out.println("This is Before advice");
    	}
    	
    	@After(value = "aspectOperations()")
    	public void after(JoinPoint joinPoint) {
    
    		System.out.println("This is After advice");
    	}
    }
    The main Class
    Code:
    /**
     * 
     */
    package ankeet.spring.tester;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import ankeet.spring.business.BusinessLogic;
    
    
    /**
     * @author ankeet
     *
     */
    public class Tester {
    	
    	public static void main(String[] args) {
    		//Loading "AOP.xml"
    		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("AOP.xml");
    		//Getting bean from the application context.
    		BusinessLogic businessLogic = (BusinessLogic) applicationContext.getBean("businessLogic");
    		//Calling the function -> iDontKnowAboutAspects()
    		businessLogic.iDontKnowAboutAspects();
    	}
    }
    AOP.xml
    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:p="http://www.springframework.org/schema/p"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
    
    	<aop:aspectj-autoproxy />
    	
    	 <bean id="businessLogic" class="ankeet.spring.business.BusinessLogic" />
       <bean id="aopAspect" class="ankeet.spring.aop.AOPAspect"/>
    </beans>
    Please help me in rectifying this. Have been stuck since quite a while.
    Last edited by ankeet.maini; Dec 18th, 2011, 11:48 PM.

  • #2
    I tired your code and it works perfectly fine.

    Comment

    Working...
    X