Spring:嵌套异常是 java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26540782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:44:56  来源:igfitidea点击:

Spring: nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

javaspring

提问by EMM

Stack trace:

堆栈跟踪:

Oct 24, 2014 8:12:04 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@50df2e: startup date [Fri Oct 24 08:12:04 IST 2014]; root of context hierarchy
Oct 24, 2014 8:12:05 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [knights-aop.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [knights-aop.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.java.spring.SpringDemo.main(SpringDemo.java:12)
Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
    at org.springframework.aop.config.ConfigBeanDefinitionParser.getAdviceClass(ConfigBeanDefinitionParser.java:410)
    at org.springframework.aop.config.ConfigBeanDefinitionParser.createAdviceDefinition(ConfigBeanDefinitionParser.java:366)
    at org.springframework.aop.config.ConfigBeanDefinitionParser.parseAdvice(ConfigBeanDefinitionParser.java:332)
    at org.springframework.aop.config.ConfigBeanDefinitionParser.parseAspect(ConfigBeanDefinitionParser.java:227)
    at org.springframework.aop.config.ConfigBeanDefinitionParser.parse(ConfigBeanDefinitionParser.java:115)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:195)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:139)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:108)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
    ... 14 more

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-3.0.xsd
        http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <bean id="knight" class="com.java.spring.BraveKnight">
    <constructor-arg ref="quest" />
  </bean>

  <bean id="quest" class="com.java.spring.ResqueDamselQuest" />

  <bean id="minstrel" 
     class="com.java.spring.Minstrel" /> <!--<co id="co_minstrel_bean"/>-->

  <aop:config>
    <aop:aspect ref="minstrel">

      <aop:pointcut id="embark" 
          expression="execution(* *.embarkOnQuest(..))" /> <!--<co id="co_define_pointcut"/>-->

      <aop:before pointcut-ref="embark"
                  method="singBeforeQuest"/>    <!--<co id="co_minstrel_before_advice"/>-->

      <aop:after pointcut-ref="embark"
                 method="singAfterQuest"/>     <!--<co id="co_minstrel_after_advice"/>-->

    </aop:aspect>
  </aop:config>
</beans>

My main class:

我的主要课程:

public class SpringDemo {
    public static void main(String[] args) {
        final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("knights-aop.xml");
        final Knight braveKnight = (Knight) applicationContext.getBean("knight");
        braveKnight.embarkOnQuest();
    }
}

I also tried adding: aopalliance.jar & aopalliance-alpha1.jar individually and together but the error won't go away.

我还尝试添加: aopalliance.jar & aopalliance-alpha1.jar 单独和一起添加,但错误不会消失。

There are similar questions here on SO: spring nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

这里有类似的问题 SO:spring 嵌套异常是 java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

###########EDIT###########Update: Other classes in the project##
Minstrel.java

###########EDIT##########Update:项目中的其他类##
Minstrel.java

package com.java.spring;

    public class Minstrel {
        public void singBeforeQuest(){
            System.out.println("Fa la la Theknight is so brave!");
        }
        public void singAfterQuest(){
            System.out.println(
                    "Tee hee he The brave knight did embark on a quest!");
        }
    }

BraveKnight.java

勇敢的骑士.java

package com.java.spring;

public class BraveKnight implements Knight {
    private Quest quest;

    BraveKnight(Quest quest) {
        this.quest = quest;
    }

    @Override
    public void embarkOnQuest() {
        quest.embark();
    }
}

RescueDamselQuest.java

RescueDamselQuest.java

package com.java.spring;

    public class RescueDamselQuest implements Quest {

        @Override
        public void embark() {
            System.out.println("Damsel rescue quest is on.");
        }
    }


I am using Spring 3.2
##################EDIT2##################
I am not using Maven. Please don't provide Maven related solution unless Spring AOP is impossible without it.
Project Snap:
enter image description here
Inside SPRING LIBRARY I have all the jar that got downloaded with Spring. Is it possible that some spring specific jar conflicting with aopalliance jar?

If anyone has some similar & simple working example without Maven, please provide it.

Thanks
~Mohit


我正在使用 Spring 3.2
#################EDIT2##################
我没有使用 Maven。请不要提供 Maven 相关的解决方案,除非没有它就无法实现 Spring AOP。
项目快照:
在此处输入图片说明
在 SPRING LIBRARY 里面,我有所有用 Spring 下载的 jar。某些特定于弹簧的 jar 是否可能与 aopalliance jar 冲突?

如果有人有一些没有 Maven 的类似和简单的工作示例,请提供。

谢谢
~莫希特

采纳答案by EMM

I removed SPRING-LIBRARY completely and then added all the required jars one by one plus cglib-nodep-2.2.jar.
There was also a spelling mistake in my code <bean id="quest" class="com.java.spring.ResqueDamselQuest" />

我完全删除了 SPRING-LIBRARY,然后将所有需要的 jar 和 cglib-nodep-2.2.jar 一一添加。
我的代码中也有拼写错误<bean id="quest" class="com.java.spring.ResqueDamselQuest" />

public class RescueDamselQuest implements Quest {

Name of class.

班级名称。

But I am sure this spelling mistake was not the reason for the exception I was getting. After adding the jars from scratch I got this error that said the bean is not found as mentioned in the knights-aop.xml sth.. sth... which is when I figured that I have this typo.

但我确信这个拼写错误不是我得到异常的原因。从头开始添加 jars 后,我得到了这个错误,说没有找到像 Knights-aop.xml 中提到的 bean …… ……这是我发现我有这个错字的时候。

This is how my jars are added now:

这就是我现在添加罐子的方式:

enter image description here

在此处输入图片说明

I hope someone might get help from this answer.

我希望有人可以从这个答案中得到帮助。

###########EDIT###########

###########编辑###########

asm-all & cglib-nodep are not required. Also, some jar from spring download was causing problems, as I tried adding all the jars I got in spring download and it started showing the old exception again. So it is better to add jar as and when required.

不需要 asm-all 和 cglib-nodep。此外,一些来自 spring 下载的 jar 导致了问题,因为我尝试添加我在 spring 下载中获得的所有 jar 并且它再次开始显示旧的异常。因此,最好在需要时添加 jar。

回答by Serge Ballesta

It looks like you only add the aopalliancejar to the build classpathof your IDE (Eclipse ?). That explains that the IDE shows no error and accepts to build the application. But in fact, you must also add it to the run classpath.

看起来您只将aopalliancejar添加到您的 IDE(Eclipse ?)的构建类路径中。这说明 IDE 没有显示错误并接受构建应用程序。但实际上,您还必须将其添加到运行类路径中

Depending on the IDE you use, another menu may allow to configure it. But if you want to run it outside the IDE, you must put the jar in your normal classpath, either by putting it along with other existing jars, or (would be better) by changing you user or system classpath to include the folder that contains the jar.

根据您使用的 IDE,另一个菜单可能允许对其进行配置。但是如果你想在 IDE 之外运行它,你必须把 jar 放在你的普通类路径中,或者将它与其他现有的 jar 放在一起,或者(会更好)通过更改用户或系统类路径以包含包含的文件夹罐子。

I could elaborate on that if you need and if I know your system ...

如果您需要并且我知道您的系统,我可以详细说明...

回答by prsutar

cross check with com.springsource.org.aopalliance-X.X.X.jar

与 com.springsource.org.aopalliance-XXXjar 交叉检查

回答by brostbeef

You're missing the dependency. Not sure how you're handling dependencies, but just add the JAR in IVY or whatever you use. You must be using something to add Spring 3.2.

你缺少依赖。不确定您如何处理依赖项,但只需在 IVY 或您使用的任何内容中添加 JAR。您必须使用某些东西来添加 Spring 3.2。

Maven Central: link

Maven 中心:链接

IVY Example: <dependency org="org.springframework" name="spring-aop" rev="3.2.11.RELEASE" />Maven has other dependency information available.

IVY 示例:<dependency org="org.springframework" name="spring-aop" rev="3.2.11.RELEASE" />Maven 有其他可用的依赖信息。

If this is a web app, make sure the JAR is in APPNAME/WEB-INF/lib.

如果这是一个 Web 应用程序,请确保 JAR 位于 APPNAME/WEB-INF/lib 中。

回答by Manuel Jordan

Be totally sure you have the following in your pom.xmlfile:

完全确定您的pom.xml文件中有以下内容:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${springframework.version}</version>
</dependency>

According with your error stack trace.

根据您的错误堆栈跟踪。

Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

The following is mandatory

以下为必填项

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${aspectj.version}</version>
</dependency>

Of course you must set or configure each version, for Spring and AOP.

当然,您必须为 Spring 和 AOP 设置或配置每个版本。

I did realize later you are not working with Maven, even with that in Maven Central Repositoryyou are able to download the jarsrequired according with my dependencies shared above

我后来确实意识到您没有使用 Maven,即使在Maven Central Repository 中,您也可以根据上面共享的依赖项下载所需的jar

回答by Raj_89

Along with aopalliance jar have you added these jars into your build path??

与 aopalliance jar 一起,您是否将这些 jar 添加到您的构建路径中?

aspectjrt
springaop
aspectjweaver

aspectjrt
springaop
aspectjweaver

If not, you can try by adding these..

P.S: use the appropriate version of jars...

如果没有,您可以尝试添加这些..

PS:使用适当版本的罐子...