springaopnoclassdeffounderrorspring-aopbeancreationexception

Throw Exception after add aop config


At first, I have a spring application works fine:

enter image description here

but when to add aop config to aop.xml like this, It throws a very lang exception. enter image description here

this is AOP.java:

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.animal.Cat;

public class AOP {
    public static void main(String args[]){

        System.out.println("1");
        BeanFactory factory = new ClassPathXmlApplicationContext("aop.xml");
        Cat c1 =(Cat)factory.getBean("cat1");
        System.out.println("2");
        c1.mark();

    }
}

this is Cat.java:

package com.animal;
public class Cat {
    public void mark(){
        System.out.println("cat is mark");
    }
}

this is Rat.java:

package com.animal;

public class Rat {
    public void sleep(){
        System.out.println("rat is sleep");
    }

    public void run(){
        System.out.println("rat running");
    }
}

this is xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:aop="http://www.springframework.org/schema/aop"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd
">


<bean id="cat1" class="com.animal.Cat">
</bean>

<bean id="rat1" class="com.animal.Rat">
</bean>

<aop:config>

    <aop:pointcut id="cat_mark" expression="execution(public void com.animal.Cat.mark())"  />

    <aop:aspect ref="rat1">
        <aop:before method="sleep" pointcut-ref="cat_mark" />
        <aop:after method="run" pointcut-ref="cat_mark" />
    </aop:aspect>

</aop:config>

As you can see, this is a very simple application, I just want to use spring AOP to make rat do some action before cat going to mark.

I have config XML file which copies from some tutorial, but I can not find what is wrong with it.


yeah, I miss aspectjweaver.jar which org.springframework.context required. This is the solution:

enter image description here


Solution

  • You have edited your question, so I am editing my answer because now I can reproduce your problem.

    Actually your original pointcut was correct. Forget what I said about it being wrong, I parsed it wrong when I first saw it. Just change it back to using only a single *, please:

    execution(* com.animal.Cat.mark(..))
    

    Now I think your problem is far simpler, judging from the callstack you posted earlier but cut off in order to replace it by a screenshot of an incomplete callstack. But I found it in your question's history, looking at the old version:

    ...
    Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
    

    I believe you just forgot to put aspectjweaver.jar (or a versioned name like aspectjweaver-1.8.13) on the classpath when running the application. Do that and it should go away. If I do that, your application prints:

    1
    Apr 20, 2018 6:04:34 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    INFORMATION: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18769467: startup date [Fri Apr 20 18:04:34 ICT 2018]; root of context hierarchy
    Apr 20, 2018 6:04:35 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFORMATION: Loading XML bean definitions from class path resource [aop.xml]
    2
    rat is sleep
    cat is mark
    rat running