添加@Aspect 抛出 BeanCreationException

Adding @Aspect throws BeanCreationException

我正在尝试在 Spring 中使用 AOP。当我在 XML 中使用旧的 DTD 方法时,一切正常。但是当我尝试使用 AspectJ 符号时,我遇到了一些问题。我从 here 下载了 aspectj 的 JAR 文件。我将它们添加到构建路径中,并且包含 @Aspect 注释的代码编译成功。但是在运行时,什么都没有执行。即使是使用基于 XML 配置的 bean,也不会被创建。它正在抛出一些 BeanCreationException。完整错误日志如下:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emp1' defined in class path resource [Beans.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emp1' defined in class path resource [Beans.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:512)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=10=](AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.demo.Tester.main(Tester.java:11)
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getPointcut(ReflectiveAspectJAdvisorFactory.java:210)
    at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisor(ReflectiveAspectJAdvisorFactory.java:192)
    at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.getAdvisors(ReflectiveAspectJAdvisorFactory.java:126)
    at org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder.buildAspectJAdvisors(BeanFactoryAspectJAdvisorsBuilder.java:110)
    at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:95)
    at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:101)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:251)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1141)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:506) ... 10 more
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

谁能帮我找出错误是什么?我知道 JVM 无法以某种方式找到 @Aspect 注释,但为什么即使在添加 JAR 文件后它也会发生?

您有一个用于下载 JAR 的奇怪网站。我建议不要手动下载依赖 JAR,而是使用像 Maven 这样的构建工具来为您处理。无论如何,如果你想让你的开发者生活更艰难,你可以手动完成。您可以在这里搜索和下载依赖项:

https://mvnrepository.com/artifact/org.aspectj/aspectjweaver

只需搜索,select您需要的版本,然后下载。

你的问题的原因是,根据你的 link URL 的猜测,你下载了 AspectJ 运行时库 aspectjrt.jar,但那个是最小的,意味着与 ApsectJ 编译时编织一起使用。您正在使用 Spring AOP,它依赖于 AspectJ 加载时编织库 aspectjweaver.jar。把那个放在你的类路径上,然后你的错误就会消失。顺便说一句,weaver 库包含运行时的超集 类,因此在这种情况下您不再需要运行时。

我还建议阅读更多文档。 Spring 手册告诉您使用编织器,而不是运行时,请参阅第 Enabling @AspectJ Support.