Spring,OpenJPA,获取bean的实例化失败;嵌套异常是java.lang.NoClassDefFoundError:当它存在时无法初始化类

时间:2011-08-20 17:21:37

标签: java spring exception maven openjpa

我有一个简单的春季应用程序在这个MediafilebrowserController工作的地方工作,我可以访问一个网址:http://localhost/item/something,它会正常工作。然后我根据我从另一个开发人员那里获得的现有项目继续将openJPA集成到项目中。在这之后,我以为我已经完成了所有设置,但现在我得到的错误如下,基本上说它找不到我的控制器类,我已经检查过并且该类确实存在于它声称无法找到它的路径在我运行应用程序的目标目录中。

我一直在寻找这个,但我发现的任何东西都没有帮助我,我想知道是否有人可以帮助我缩小可能导致这种情况的原因。感谢

我的根本原因是异常:

full stracktrace: http://pastie.org/2402614

 org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'mediafilebrowserController' defined in file 
[/home/user/workspace/mediaModule/target/MediaFileBrowser-0.0.1-SNAPSHOT/WEB-INF/classes/com/company/mediafilebrowser/spring/controller/MediafilebrowserController.class]: 
Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: 
Could not initialize class com.company.mediafilebrowser.spring.controller.MediafilebrowserController

(易于阅读的换行)

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
            org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
            org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
            org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
            org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
            org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
            org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
            org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
            org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
            org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
            org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
            org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
            org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
            javax.servlet.GenericServlet.init(GenericServlet.java:212)
            org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
            org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
            org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
            org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
            java.lang.Thread.run(Thread.java:636)

这是我的控制器类:

package com.company.mediafilebrowser.spring.controller;

import javax.servlet.http.HttpSession;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.velocity.VelocityView;

@Controller

@RequestMapping("/item")

public class MediafilebrowserController {

    @RequestMapping(value = "/{testinput}")

    public String temp(@PathVariable("testinput") String testinput,

            HttpSession session, Model model) {

        session.setAttribute("testattribute", testinput);

        System.out.println("It is working");
        return "something";

    }
}

我的servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Scans within the base package of the application for @Components to 
        configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
    <context:component-scan base-package="com.company.mediafilebrowser.spring.controller" />
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/jsp" p:suffix=".jsp" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="mediafilebrowser" />
    </bean>

        <bean id="entityManagerResourceLoader"
        class="com.company.mediafilebrowser.spring.resourceloader.EntityManagerResourceLoader"
        p:entityManagerFactory-ref="entityManagerFactory" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="uploadedFileDAO" class="com.company.mediafilebrowser.uploadedfile.dao.UploadedFileDAO">
        <property name="entityManagerFactory">
            <ref bean="entityManagerFactory" />
        </property>
    </bean>

    <bean id="uploadedFileService" class="com.company.mediafilebrowser.uploadedfile.service.UploadedFileService">
        <property name="uploadedFileDAO">
            <ref bean="uploadedFileDAO" />
        </property>
    </bean>



</beans>

我的POM:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>MediaFileBrowser</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

        <dependencies>
        <!-- <dependency> <groupId>net.disbelieve</groupId> <artifactId>joffree</artifactId> 
            <version>0.6.1</version> </dependency> -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.2.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.0.5.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-all</artifactId>
            <version>2.1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>drools</groupId>
            <artifactId>drools-all</artifactId>
            <version>2.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.jbpm</groupId>
            <artifactId>jbpm-persistence-jpa</artifactId>
            <version>5.1.0.CR1</version>
            <type>jar</type>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-annotations</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jbpm</groupId>
            <artifactId>jbpm-bam</artifactId>
            <version>5.1.0.CR1</version>
            <type>jar</type>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-core</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jbpm</groupId>
            <artifactId>jbpm-bpmn2</artifactId>
            <version>5.1.0.CR1</version>
            <type>jar</type>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-core</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-persistence-jpa</artifactId>
            <version>5.2.0.CR1</version>
            <type>jar</type>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-annotations</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-commons-annotations</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>hibernate-core</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.btm</groupId>
            <artifactId>btm</artifactId>
            <version>2.1.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.16</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.0.5.RELEASE</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>opensymphony</id>
            <url>https://maven.atlassian.com/content/repositories/atlassian-3rdparty</url>
        </repository>
        <repository>
            <id>langhua</id>
            <url>http://maven2.langhua.org/public</url>
        </repository>
        <repository>
            <id>jboss</id>
            <url>https://repository.jboss.org/nexus</url>
        </repository>
    </repositories>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>1.2</version>
                <configuration>
                    <includes>com/kryterion/mediafilebrowser/**/model/*.class</includes>
                    <addDefaultConstructor>true</addDefaultConstructor>
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                    <!-- Pass additional properties to the Plugin here <toolProperties> 
                        <property> <name>directory</name> <value>otherdirectoryvalue</value> </property> 
                        </toolProperties> -->
                </configuration>
                <executions>
                    <execution> 
                        <id>enhancer</id> 
                        <phase>process-classes</phase> 
                        <goals> 
                            <goal>enhance</goal> 
                        </goals> 
                    </execution> 
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa-all</artifactId>
                        <version>2.1.0</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>emma-maven-plugin</artifactId>
                <version>1.0-alpha-3</version>
                <inherited>true</inherited>
                <executions>
                    <execution> 
                        <phase>process-classes</phase> 
                        <goals> 
                            <goal>instrument</goal> 
                        </goals> 
                    </execution> 
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <forkMode>once</forkMode>
                    <reportFormat>xml</reportFormat>
                    <classesDirectory>${project.build.directory}/generated-classes/emma/classes</classesDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>TestDelivery</warName>
                </configuration>
                <version>2.1.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>emma-maven-plugin</artifactId>
                <version>1.0-alpha-3</version>
            </plugin>
        </plugins>
    </reporting>
</project>

1 个答案:

答案 0 :(得分:0)

servlet-context.xml中,使用bean定义控制器类:

<bean id="mediafilebrowserController" class="com.company.mediafilebrowser.spring.controller.MediafilebrowserController"/>