创建名为' hibernate4AnnotatedSessionFactory'的bean时出错。在ServletContext资源中定义[/WEB-INF/dispatcher-servlet.xml]:

时间:2018-05-25 17:12:41

标签: java eclipse hibernate spring-mvc pom.xml

  

引起:org.springframework.beans.factory.BeanCreationException:   无法自动装配字段:   com.programcreek.helloworld.controller.CountryService   com.programcreek.helloworld.controller.CountryController.countryService;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   使用名称' countryService'创建bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:com.programcreek.helloworld.controller.CountryDAO   com.programcreek.helloworld.controller.CountryService.countryDao;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   使用名称' countryDAO'创建bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   com.programcreek.helloworld.controller.CountryDAO.sessionFactory;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为' hibernate4AnnotatedSessionFactory'定义于   ServletContext资源[/WEB-INF/dispatcher-servlet.xml]:   bean的初始化失败;嵌套异常是   org.springframework.beans.TypeMismatchException:无法转换   类型' java.util.ArrayList'的属性值要求的类型   ' java.lang.Class中[]'对于属性' annotatedClasses&#39 ;;嵌套异常   是java.lang.IllegalArgumentException:找不到类   [org.arpit.java2blog.model.Country]

的pom.xml

<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>test</groupId>
  <artifactId>test</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>test Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <spring.version>4.1.6.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Spring dependencies -->
        <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>

        <dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
        <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
        <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.24</version>
</dependency>

        <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.0.1.Final</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.0.1.Final</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>
        <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>
    </dependencies>
  <build>
    <finalName>test</finalName>
  </build>
</project>

dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    <context:component-scan base-package="com.programcreek.helloworld.controller" />
 <annotation-driven />
    <resources mapping="/resources/**" location="/resources/" />

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

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

    <beans:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>org.arpit.java2blog.model.Country</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

        <tx:annotation-driven transaction-manager="transactionManager" />
    <beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory"
            ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
</beans:beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">


    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///

1 个答案:

答案 0 :(得分:1)

要解决Spring依赖项问题,请导航到根嵌套异常。通常,自动连接将是一系列嵌套异常,依赖关系无法自动连接,因此特定的bean故障将隐藏在原始异常日志的底部。在这种情况下......

  

找不到类[org.arpit.java2blog.model.Country]

由于它未在pom.xml中列为可能的orp.arpit.java2blog依赖项,因此它必须位于src/main/java目录或Maven项目本身内的任何其他已定义的源目录中。该类在servlet中输入错误,或者文件未正确放入项目中。