Spring AOP BeanCreationException 时添加一个简单的 Before 建议



我是Spring AOP的新手。我用一个简单的@Before建议写了一个方面,看起来像这样

package testing_spring;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyTestAspect {
@Before("execution(public void open())")
public void runThis() {
System.out.println("About to open something ...");
}
}

这是上下文.xml文件:

<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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean class="testing_spring.House" name="house">
<property name="windows">
<list>
<ref bean="window1"/>
<ref bean="window2"/>
<ref bean="window3"/>
</list>
</property>
<property name="doors">
<list>
<ref bean="door"/>
</list>
</property>
</bean>
<bean class="testing_spring.Window" name="window1" id="window1"/>
<bean class="testing_spring.Window" name="window2" id="window2"/>
<bean class="testing_spring.Window" name="window3" id="window3"/>
<bean class="testing_spring.Door" name="door" id="door"/>

<bean class="testing_spring.MyTestAspect" name="mta"/>
</beans>

其余的只是一些用于测试的简单类:

package testing_spring;
public abstract class Openable {
private boolean open;
public void open() {
open = true;
}
public void close() {
open = false;
}
public boolean isOpen() {
return open;
}
}
package testing_spring;
public class Door extends Openable {
}
package testing_spring;
public class Window extends Openable {
}
import java.util.*;
public class House {
private List<Window> windows;
private List<Door> doors;
public void setWindows(List<Window> windows) {
this.windows = windows;
}
public void setDoors(List<Door> doors) {
this.doors = doors;
}
public List<Window> getWindows() {
return windows;
}
public List<Door> getDoors() {
return doors;
}
}
package testing_spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
House house = (House) ctx.getBean("house");
printHouseInfo(house);
}
public static void printHouseInfo(House house) {
System.out.println("My house has " + house.getDoors().size() + " doors and " + house.getWindows().size() + " windows.");
}
}

所以,当我运行main方法时,我得到的是:

cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'house' defined in class path resource [context.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

但是当我注释掉@Before注释时,一切正常,它会打印:

My house has 1 doors and 3 windows.

所以之前的建议一定有问题,但我不明白,它是什么。

您可能没有为 aspectj-weaver 添加类路径.jar

最新更新