org.springframework.beans.factory.CannotLoadBeanClassException:找不到类

时间:2010-02-12 04:38:49

标签: java spring-mvc

我从tomcat 6.0安装目录中用servle-api.com替换了j2ee.jar: 这会产生下面的错误。我现在正试图找出原因。可能是什么问题。

我在配置文件中定义了一个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>

我的 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>

堆栈跟踪

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

8 个答案:

答案 0 :(得分:15)

问题是您的webapp的类路径中没有名为com.service.SempediaSearchManager的类。最可能的根本原因是:

  • /WEB-INF/Sempedia-service.xml中的完全限定类名不正确;即班级名称是别的,

  • 该类不在您的webapp的/WEB-INF/classes目录树或/WEB-INF/lib目录中的JAR文件中。

编辑:我唯一能想到的是ClassDefNotFoundException实际上可能是早期类加载/静态初始化问题的结果。检查日志文件中的第一个堆栈跟踪,并查看嵌套异常,即“由...引起”链。 [如果类加载失败了一次,而你或Spring因某种原因再次调用Class.forName(),那么Java实际上不会尝试第二次加载。相反,您将获得ClassDefNotFoundException堆栈跟踪,但不能解释原始故障的真正原因。]

如果你仍然难过,你应该把Eclipse从图片中删除。以最终要部署它的形式创建WAR文件,然后从命令行创建:

  1. 手动关闭Tomcat

  2. 清理您的Tomcat webapp目录,

  3. 将WAR文件复制到webapp目录中,

  4. 启动Tomcat。

  5. 如果这不能直接解决问题,请查看Tomcat上已部署的webapp目录,以验证“缺失”类是否在正确的位置。

答案 1 :(得分:2)

我得到了同样的错误,原因是目录:

  

U:..... WEB \的WebRoot \ WEB-INF \类\ COM \ yourcompany \ CC \ DAO

已损坏(目录或文件无法读取或损坏) 解决了

  • 将目录WEB-INF \ classes重命名为WEB-INF \ classes_old
  • Eclipse的项目菜单 - &gt;清理(​​重新创建目录)
  • 重新部署 - &gt;重启服务器。

答案 2 :(得分:1)

我遇到了同样的问题。当我检查build / classes文件夹时,没有生成任何类。按照规定here将Maven Dependency添加到“部署Web程序集”之后 问题已经解决。

答案 3 :(得分:1)

检查你的依赖关系。

<?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>

答案 4 :(得分:0)

我有同样的问题。我根据 Stephen的建议检查了我的/ WEB-INF /课程:

  

该类不在您的webapp的/ WEB-INF / classes目录树中,也不在/ WEB-INF / lib目录中的JAR文件中。

我发现我有一个过时的jar文件。用最新的jar文件替换它解决了这个问题。

答案 5 :(得分:0)

将此留给未来的访客:

在我的情况下,缺少/ WEB-INF / classes目录。如果您使用的是Eclipse,请确保.settings / org.eclipse.wst.common.component正确(项目设置中的Deployment Assembly)。

在我的情况下它缺少

    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>

此文件也是Anuj提到的常见错误源(缺少其他项目的依赖关系)。

否则,希望其他答案(或“问题”标签)可以帮助您。

答案 6 :(得分:0)

我也遇到过类似的问题

所以

我只是做

1.update Maven项目

2。安装maven。

答案 7 :(得分:0)

我不知道这是否与您的问题有关,我也遇到了类似的问题,

1)在Eclipse中,右键单击服务器并清理

如果仍然无法正常工作

2)导出项目并删除该项目,创建具有相同名称的项目,然后导入该项目并将该项目添加到服务器并运行。