Java org.springframework.beans.factory.CannotLoadBeanClassException:找不到类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2249697/
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
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class
提问by Terman
I replaced j2ee.jar with servle-api.com from my tomcat 6.0 installation directory: And that yields the error below. I'm presently trying to figure out the cause. What might the problem be.
我从我的 tomcat 6.0 安装目录中用 servle-api.com 替换了 j2ee.jar: 这产生了下面的错误。我目前正在尝试找出原因。可能是什么问题。
I have a the bean defined in a configuration file: Sempedia-service.xml as follows
我在配置文件中定义了一个 bean:Sempedia-service.xml 如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="sempediaSearchService"
class="com.service.SempediaSearchManager" />
</beans>
my web.xmlspecifies the following beans:
我的web.xml指定了以下 bean:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Sempedia-service.xml,/WEB-INF/Sempedia-persistence.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Stack Trace
堆栈跟踪
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.service.SempediaSearchManager] for bean with name 'sempediaSearchService' defined in ServletContext resource [/WEB-INF/Sempedia-service.xml]; nested exception is java.lang.ClassNotFoundException: com.service.SempediaSearchManager
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1141)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:758)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:422)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.ClassNotFoundException: com.service.SempediaSearchManager
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:211)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:385)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
... 24 more
采纳答案by Stephen C
The problem is that there is no class called com.service.SempediaSearchManager
on your webapp's classpath. The most likely root causes are:
问题是com.service.SempediaSearchManager
在您的 web 应用程序的类路径上没有调用任何类。最可能的根本原因是:
the fully qualified classname is incorrect in
/WEB-INF/Sempedia-service.xml
; i.e. the class name is something else,the class is not in your webapp's
/WEB-INF/classes
directory tree or a JAR file in the/WEB-INF/lib
directory.
中的完全限定类名不正确
/WEB-INF/Sempedia-service.xml
;即类名是别的东西,该类不在您的 web 应用程序的
/WEB-INF/classes
目录树或目录中的 JAR 文件中/WEB-INF/lib
。
EDIT: The only other thing that I can think of is that the ClassDefNotFoundException
may actually be a result of an earlier class loading / static initialization problem. Check your log files for the firststack trace, and look the nested exceptions, i.e. the "caused by" chain. [If a class load fails one time and you or Spring call Class.forName()
again for some reason, then Java won't actually try to load a second time. Instead you will get a ClassDefNotFoundException
stack trace that does not explain the real cause of the original failure.]
编辑:我能想到的唯一另一件事是这ClassDefNotFoundException
可能实际上是早期类加载/静态初始化问题的结果。检查您的日志文件中的第一个堆栈跟踪,并查看嵌套的异常,即“由”链。[如果类加载一次失败并且您或 SpringClass.forName()
由于某种原因再次调用,那么 Java 实际上不会尝试加载第二次。相反,您将得到一个ClassDefNotFoundException
堆栈跟踪,它不能解释原始失败的真正原因。]
If you are still stumped, you should take Eclipse out of the picture. Create the WAR file in the form that you are eventually going to deploy it, then from the command line:
如果您仍然难倒,您应该将 Eclipse 排除在外。以您最终要部署它的形式创建 WAR 文件,然后从命令行:
manually shutdown Tomcat
clean out your Tomcat webapp directory,
copy the WAR file into the webapp directory,
start Tomcat.
手动关闭Tomcat
清理你的Tomcat webapp目录,
将 WAR 文件复制到 webapp 目录中,
启动Tomcat。
If that doesn't solve the problem directly, look at the deployed webapp directory on Tomcat to verify that the "missing" class is in the right place.
如果这样不能直接解决问题,请查看 Tomcat 上部署的 webapp 目录以验证“缺少”的类是否在正确的位置。
回答by chris
I have the same problem. I checked my /WEB-INF/classes based on Stephen'srecommendation:
我也有同样的问题。我根据斯蒂芬的建议检查了我的 /WEB-INF/classes :
the class is not in your webapp's /WEB-INF/classes directory tree or a JAR file in the /WEB-INF/lib directory.
该类不在您的 web 应用程序的 /WEB-INF/classes 目录树或 /WEB-INF/lib 目录中的 JAR 文件中。
I discovered I have an outdated jar file. Replacing it with the latest jar file solved the issue.
我发现我有一个过时的 jar 文件。用最新的 jar 文件替换它解决了这个问题。
回答by user2869492
Check your dependencies.
检查您的依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SchoolApp</groupId>
<artifactId>SchoolApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<hibernate.version>4.2.0.Final</hibernate.version>
<mysql.connector.version>5.1.21</mysql.connector.version>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
<dependencies>
<!-- DB related dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.3.RELEASE</version>
</dependency>
<!-- CGLIB is required to process @Configuration classes -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Servlet API and JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test-mvc</artifactId>
<version>1.0.0.M1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-maven-milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
</repository>
</repositories>
<build>
<finalName>spr-mvc-hib</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
回答by arn-arn
i recently experienced this issue when i was trying to build and suddenly the laptop battery ran out of charge. I just removed all the servers from eclipse and then restore it. after doing that, i re-built the war file and it worked.
我最近在尝试构建时遇到了这个问题,突然笔记本电池没电了。我刚刚从 eclipse 中删除了所有服务器,然后将其恢复。这样做之后,我重新构建了war文件并且它起作用了。
回答by Markus Barthlen
Just leaving this here for future visitors:
只是把这个留在这里给未来的访客:
In my case the /WEB-INF/classes directory was missing. If you are using Eclipse, make sure the .settings/org.eclipse.wst.common.component is correct (Deployment Assembly in the project settings).
在我的情况下, /WEB-INF/classes 目录丢失了。如果您使用的是 Eclipse,请确保 .settings/org.eclipse.wst.common.component 是正确的(项目设置中的部署程序集)。
In my case it was missing
就我而言,它不见了
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>
This file is also a common source of errors as mentioned by Anuj (missing dependencies of other projects).
该文件也是 Anuj 提到的常见错误来源(缺少其他项目的依赖项)。
Otherwise, hopefully the other answers (or the "Problems" tab) will help you.
否则,希望其他答案(或“问题”选项卡)会对您有所帮助。
回答by Legolas
I got the same error and the cause was the directory:
我遇到了同样的错误,原因是目录:
U:.....WEB\WebRoot\WEB-INF\classes\com\yourcompany\cc\dao
U:...WEB\WebRoot\WEB-INF\classes\com\yourcompany\cc\dao
was corrupted(directory or file not readable or damaged).. solved with
已损坏(目录或文件不可读或损坏)..已解决
- renaming the directory WEB-INF\classes as WEB-INF\classes_old
- Eclipse's Project menu--> Clean (to recreate directories)
- redeploy --> restart server.
- 将目录 WEB-INF\classes 重命名为 WEB-INF\classes_old
- Eclipse 的 Project 菜单--> Clean(重新创建目录)
- 重新部署 --> 重新启动服务器。
回答by Abhijeet Kale
I have faced similar problem
我遇到过类似的问题
So
所以
I just do
我只是做
1.update Maven Project
1.更新Maven项目
2.install maven.
2.安装maven。
回答by Sush Shanth
i dont know whether it is relevant to your issue, i got similar issue which i got solved by
我不知道它是否与您的问题有关,我遇到了类似的问题,我已经解决了
1) In eclipse right click server and clean
1)在eclipse中右键单击服务器并清理
if it still didnt work
如果它仍然不起作用
2) export the project and delete the project create the project with same name and import the project and add the project to server and run.
2)导出项目并删除项目创建同名项目并导入项目并将项目添加到服务器并运行。