【问题标题】:Hibernate Configuration, Exception encountered during context initialization, Errors with HomeController and sessionFactoryHibernate 配置,上下文初始化期间遇到异常,HomeController 和 sessionFactory 出错
【发布时间】:2017-11-04 22:14:50
【问题描述】:

首先,我最近尝试在 JAVA EE 中提高自己。还有另一个话题,实际上和我的问题一样。但是该解决方案对我不起作用。这是该主题的链接:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory

另外一个话题是: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

无论如何我都会提供我所面临的错误,

错误信息:

Org.springframework.beans.factory.UnsatisfiedDependencyException: Error >creating bean with name 'homeController': Unsatisfied dependency expressed >through field 'productDao;

嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“sessionFactory”的 bean 时出错:无法自省 bean 类

这里是 HomeController:

package com.emusicstore.controller;

import com.emusicstore.dao.ProductDao;
import com.emusicstore.model.Product;
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 java.io.IOException;
import java.util.List;

@Controller
public class HomeController {

    @Autowired
    private ProductDao productDao;

    @RequestMapping("/")
    public String home(){
        return "home";
    }

    @RequestMapping("/productList")
    public String getProducts(Model model) {
        List<Product> products = productDao.getAllProducts();
        model.addAttribute("products", products);

        return "productList";
    }

    @RequestMapping("/productList/viewProduct/{productId}")
    public String viewProduct(@PathVariable String productId, Model model) throws IOException {
        Product product = productDao.getProductById(productId);
        model.addAttribute(product);
        return "viewProduct";
    }
}

Pom.xml :

<groupId>com.mywebsite</groupId>
<artifactId>emusicstore</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.195</version>
    </dependency>

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>

</dependencies>

ApplicationContext.xml:

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:tcp://localhost/~/test" />
    <property name="username" value="sa" />
    <property name="password" value="" />
 </bean>


<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties" >

        <props>
            <prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql" >true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>

    <property name="packagesToScan">
        <list>
            <value>com.emusicstore</value>
        </list>
    </property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

我尝试过的事情:

  • 在 applicationContext.xml 中注册 bean 对象

bean id="productDao" class="com.emusicstore.dao.impl.ProductDaoImpl" />

  • 使用@Component 注释 ProductDao 类

  • 将 Hibernate Core jar 文件添加到项目结构中

这也是我的项目结构的图片: Project Structure

完整的错误堆栈:

WARNING [RMI TCP Connection(3)-127.0.0.1] 
org.springframework.web.context.support.XmlWebApplicationContext.refresh 
Exception encountered during context initialization - cancelling refresh 
attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'homeController': Unsatisfied dependency 
expressed through field 'productDao'; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'sessionFactory': Failed to introspect bean class 
[org.springframework.orm.hibernate4.LocalSessionFactoryBean] for lookup 
method metadata: could not find class that it depends on; nested 
exception is java.lang.NoClassDefFoundError: org/hibernate
/cfg/Configuration

SEVERE [RMI TCP Connection(3)-127.0.0.1]  
org.springframework.web.context.ContextLoader.initWebApplicationContext 
Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'homeController': Unsatisfied dependency 
expressed through field 'productDao'; 
nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'sessionFactory': 
Failed to introspect bean class 
[org.springframework.orm.hibernate4.LocalSessionFactoryBean] for lookup    
method metadata: 
could not find class that it depends on; nested exception is   
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

【问题讨论】:

  • 您可以将&lt;context:component-scan base-package="com.emusicstore" /&gt; 添加到您的`applicationContext.xml` 中并尝试一下吗?
  • 不,它不起作用。
  • 好的。你有同样的错误吗?
  • 是的,一模一样。
  • 你能粘贴完整的错误堆栈跟踪吗?

标签: java spring hibernate spring-mvc tomcat


【解决方案1】:

尝试添加

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-tools</artifactId>
    <version>4.0.1.Final</version>
</dependency>

给你pom.xml文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2018-01-26
    • 2018-09-08
    • 2020-10-10
    相关资源
    最近更新 更多