gpt4 book ai didi

java - Hibernate 配置,上下文初始化期间遇到异常,HomeController 和 sessionFactory 出错

转载 作者:行者123 更新时间:2023-11-28 22:26:00 25 4
gpt4 key购买 nike

首先,我最近想在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;

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Failed to introspect bean class

这是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 核心 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

最佳答案

尝试添加

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

给你pom.xml文件

关于java - Hibernate 配置,上下文初始化期间遇到异常,HomeController 和 sessionFactory 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com