Spring 抛出 NoClassDefFoundError: MethodInterceptor 尽管类存在于类路径中

发布于 2025-01-02 13:04:13 字数 2520 浏览 1 评论 0原文

我正在使用 Spring MVC 和 Hibernate 开发一个简单的培训应用程序。我使用 Maven 作为构建工具。 所有依赖项(spring、hibernate、aopalliance、junit 等)均使用 Maven 的 pom.xml 文件进行解析。

$ mvn war:war glassfish:deploy 工作绝对正常,该项目正在部署到 GlassFish 服务器 - 所有 *.jar 文件都被复制(包括 com. springsource.org.aopalliance-1.0.0.jar)。

我制作了一个简单的 servlet 来测试类路径中是否存在 aopalliance:

protected void doGet(...) throws ... {
    response.getWriter().println(org.aopalliance.intercept.MethodInterceptor.class.getCanonicalName());
}

并且它存在。上面的代码按预期显示 org.aopalliance.intercept.MethodInterceptor 。

但是,如果我将 servlet 更改为类似的内容:

protected void doGet(...) throws ... {
    response.getWriter().println(org.springframework.transaction.interceptor.TransactionInterceptor.class.getCanonicalName());
}

它会抛出异常:

java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

TransactionInterceptor 使用 aopalliance 接口,但我不明白为什么它找不到它们,而我的 servlet 可以。我相信它可能与类加载器有某种关系,但我担心我不知道如何修复它。

编辑:

一些详细信息:

编辑:

我还按照@Ravi的建议添加了 spring.osgi.core/io 的依赖项:

<dependency>
    <groupId>org.springframework.osgi</groupId>
    <artifactId>org.springframework.osgi.core</artifactId>
    <version>1.2.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.osgi</groupId>
    <artifactId>org.springframework.osgi.io</artifactId>
    <version>1.2.1</version>
</dependency>

但它没有修复 问题。

不过,我尝试在随 SpringSource Tool Suite 提供的 VMware vFabric tc Server 上运行完全相同的应用程序,一切都运行良好。这似乎是 GlassFish 特有的问题。

我正在使用 GlassFish Server 开源版 3.1.1。

另一个奇怪的事情:如果我重新部署应用程序(在 Eclipse 中使用“发布”),servlet 会抛出:

java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

但刷新后(在浏览器中)我得到:

java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/TransactionInterceptor

进一步刷新不会改变任何内容。

I'm developing a simple, training application with Spring MVC and Hibernate. I'm using Maven as build tool.
All dependencies (spring, hibernate, aopalliance, junit etc.) are resolved using Maven's pom.xml file.

$ mvn war:war glassfish:deploy works absolutley fine, the project is being deployed to the GlassFish server - all *.jar files are copied (including com.springsource.org.aopalliance-1.0.0.jar).

I've made a simple servlet to test whether aopalliance exisists within classpath:

protected void doGet(...) throws ... {
    response.getWriter().println(org.aopalliance.intercept.MethodInterceptor.class.getCanonicalName());
}

And it exists. The above code displays org.aopalliance.intercept.MethodInterceptor as expected.

However if I change the servlet into something like that:

protected void doGet(...) throws ... {
    response.getWriter().println(org.springframework.transaction.interceptor.TransactionInterceptor.class.getCanonicalName());
}

It throws an exception:

java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

TransactionInterceptor uses aopalliance interfaces, but I don't understand why it cannot find them, while my servlet can. I believe it might be somehow related to the classloader, but I'm affraid I have no idea how to fix it.

EDIT:

Some details:

EDIT:

I've also added dependencies for spring.osgi.core/io as suggested by @Ravi:

<dependency>
    <groupId>org.springframework.osgi</groupId>
    <artifactId>org.springframework.osgi.core</artifactId>
    <version>1.2.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.osgi</groupId>
    <artifactId>org.springframework.osgi.io</artifactId>
    <version>1.2.1</version>
</dependency>

But it didn't fix the problem.

However, I've tried to run the very same application on VMware vFabric tc Server, which is delivered with SpringSource Tool Suite, and everything worked just fine. This seems to be GlassFish-specific issue.

I'm using GlassFish Server Open Source Edition 3.1.1.

Another strange thing: if I redeploy application (using "Publish" in Eclipse) the servlet throws:

java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

But after refresh (whitin a browser) I get:

java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/TransactionInterceptor

Further refreshes don't change anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城泪 2025-01-09 13:04:13

我遇到了同样的问题,在寻找答案近一周后,我发现您需要 aopalliance.jar。这解决了我的问题。

I had the same issue and after hunting for answers for almost a week, I found out that you need aopalliance.jar. This solved my problem.

盛夏尉蓝 2025-01-09 13:04:13

您可能在父类加载器中的某个位置有另一个不完整的 spring,最有可能在 {domaindir}/lib 中

You might have another and incomplete spring somewhere in parent classloader, most likely in {domaindir}/lib

能怎样 2025-01-09 13:04:13

您是否已将 Spring 事务 jar 包含在类路径中?
TransactionInterceptor.java 的源代码包含对一些 org.springframework.transaction 和 org.springframework.transaction.support 包的引用。

在你的第一个片段中
org.aopalliance.intercept.MethodInterceptor.class.getCanonicalName() 您仅加载 aopalliance 类,该类不依赖于 Spring Transaction 库。

在第二个片段中,您将加载 TransactionInterceptor 类及其依赖项 (org.springframework.transaction.interceptor.TransactionInterceptor.class.getCanonicalName())。
与之前的错误(浏览器刷新之前)相比,浏览器刷新后看到的第二个异常是有意义的。

第一个片段是一个独立的类,但第二个片段是一个 Spring 类加载,它是 aopalliance 的包装器。 Spring 正在尝试加载自己的依赖项(事务相关的类和 aopalliance 实现)。

尽管在编译时找到了它的依赖项之一,但在运行时未找到它时,会引发 java.lang.NoClassDefFoundError

尝试添加这些依赖项并检查它是否解决。

Have you included Spring transaction jars in your classpath.
The source for TransactionInterceptor.java contains reference to some of the org.springframework.transaction and org.springframework.transaction.support packages.

In your first snippet
org.aopalliance.intercept.MethodInterceptor.class.getCanonicalName() you are only loading the aopalliance class which has no dependencies on the Spring Transaction libraries.

In the second snippet you are loading TransactionInterceptor class and its dependencies (org.springframework.transaction.interceptor.TransactionInterceptor.class.getCanonicalName()).
The second exception you see after browser refresh makes sense compared to previous error (before browser refresh)

The first snippet is a independent class but the second snippet is a Spring class load which is a wrapper over aopalliance. Spring is trying to load its own dependencies (transaction related classes and aopalliance implementation).

java.lang.NoClassDefFoundError is thrown when one of its dependencies is not found at runtime although its found at compile time (dependency issues)

Try adding these dependencies and check if it resolves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文