我想一起使用 AspectJ AOP 和 Spring(对于 DI),但我得到以下异常:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class
我使用 IntelliJ IDEA 12 Ultimate IDE。
以下是重现错误的示例步骤。
1:信息界面:
package org.example.bugs.bug;
public interface Info {
public void info();
}
2:接口实现:
package org.example.bugs.bug;
public class Informer implements Info {
@Override
public void info() {
System.out.println("Some info from Informer!");
}
}
3:看点:
package org.example.bugs.bug;
public aspect InfoAspect {
public InfoAspect() {}
pointcut info() : execution(* org.example.bugs.bug.Informer.info(..));
after() returning() : info() {
System.out.println("Information confirmed by InfoAspect!");
}
}
4:spring-config.xml文件:
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy />
<bean id="informer"
class="org.example.bugs.bug.Informer"/>
<bean class="org.example.bugs.bug.InfoAspect"
factory-method="aspectOf"/>
</beans>
5:我在以下 Main 类中运行所有内容:
package org.example.bugs.bug;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("org/example/bugs/bug/spring-config.xml");
Info i = (Info) context.getBean("informer");
i.info();
}
}
...我得到错误:
2013-03-24 15:46:10 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@f81843: startup date [Sun Mar 24 15:46:10 CET 2013]; root of context hierarchy
2013-03-24 15:46:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [org/example/bugs/bug/spring-config.xml]
2013-03-24 15:46:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@13ad085: defining beans [org.springframework.aop.config.internalAutoProxyCreator,informer,org.example.bugs.bug.InfoAspect#0]; root of factory hierarchy
2013-03-24 15:46:10 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@13ad085: defining beans [org.springframework.aop.config.internalAutoProxyCreator,informer,org.example.bugs.bug.InfoAspect#0]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.example.bugs.bug.InfoAspect] for bean with name 'org.example.bugs.bug.InfoAspect#0' defined in class path resource [org/example/bugs/bug/spring-config.xml]; nested exception is java.lang.ClassNotFoundException: org.example.bugs.bug.InfoAspect
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1266)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:629)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:578)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:901)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.example.bugs.bug.Main.main(Main.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.example.bugs.bug.InfoAspect
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:260)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1287)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1258)
... 15 more
我做错了什么?