Spring错误创建bean FileNotFoundException

时间:2016-02-15 23:15:00

标签: java spring hibernate maven spring-mvc

我在Spring和Hibernate中使用Intellij。 每当我使用Tomcat部署我的应用程序时,我都会收到这个奇怪的错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController' defined in file [C:\Users\Pawel\IdeaProjects\Spring2\target\com.pawel-1.0-SNAPSHOT\WEB-INF\classes\com\pawel\controller\TestController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.pawel.controller.TestController]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/TestServlet-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/TestServlet-servlet.xml] cannot be opened because it does not exist

但我确实有那个档案。即使我在“... context = new ClassPathXmlApplicationContext(”“)中执行CTRL + SPACE,它也会检测到它并自动完成它,如下所示。我问过朋友,但没有人可以帮助我。而且,这个问题发生了,当我在spring.xml中添加其他bean时,当我只有最后一个bean(没有休眠)时问题没有发生

网络浏览器中显示的错误是 HTTP状态500 - Servlet TestServlet的Servlet.init()抛出异常

My files

我的控制器

    package com.pawel.controller;

import com.pawel.dao.PersonDAO;
import com.pawel.model.Person;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class TestController {

    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/TestServlet-servlet.xml");

    PersonDAO personDAO = context.getBean(PersonDAO.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String testView(ModelMap model){
        Person person = new Person();
        person.setAge(10);
        person.setName("John");
        person.setLastName("Smith");
        personDAO.save(person);
        model.addAttribute("person", person);
        model.addAttribute("message", "Hello");
        return "test";
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

TestServlet-servlet.xml(spring文件)

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.pawel" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/db" />
        <property name="username" value="root" />
    </bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <bean id="hibernate4AnnotatedSessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.pawel.model.Person</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>


    <bean id="personDAO" class="com.pawel.dao.PersonDAOImpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    </beans>

pom.xml(maven)

    <?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>Spring2</groupId>
    <artifactId>com.pawel</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- hibernate-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
    </dependencies>

</project>

2 个答案:

答案 0 :(得分:0)

带有错误消息 nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/TestServlet-servlet.xml] cannot be opened because it does not exist =&gt; tomcat没有检测到文件。

你必须将他置于WEB-INF以下,你必须在~\tomcat\webapps下部署你的战争/ jar。部署它时,您将拥有一个与war \ jar名称相同的文件夹,您可以检查TestServlet-servlet.xml是否存在?你也可以用winrar打开war / jar来查看你的文件是否存在。

答案 1 :(得分:0)

回答您的问题

  1. 请放入src / test / resources文件夹中的TestServlet-servlet.xml并打包war文件。它允许您的应用程序上下文找到TestServlet-servlet.xml文件。
  2. 请参考下图。
  3. Test