- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我最近想在JAVA EE上提高自己。而且还有一个题目,居然和我的问题一样。但是解决方案对我不起作用。这是该主题的链接:
另外一个话题是: 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>
我尝试过的事情:
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/
我正在使用 Symfony 3.4,当我尝试运行服务器时出现错误: In RegisterControllerArgumentLocatorsPass.php line 68: Class "AppB
好的,我一直在关注: http://railscasts.com/episodes/196-nested-model-form-part-1 以下是我迄今为止必须完成的步骤: rails new su
我的网络应用程序出现此错误: InvalidArgumentException in ControllerResolver.php line 160: Class "SocialWall/Contro
我正在从 scrach Symfony 学习 5.0.1 版本。我使用 --full 选项创建了一个项目。注释包已安装。 这是我的错误: Class "1\HomeController" does n
这是我第一次部署到heroku。我尝试使用 postgreSql 项目部署 Spring MVC。 当我第一次部署到heroku时,它说我缺少Procfile。然后我添加了带有文本的 Procfile
这是 asp.net mvc3。 当我尝试转到主页/索引操作时: public class HomeController : Controller { private
如标题所述,我想在 .NET Core 2.1 中实现某种授权,但我无法按照他们的教程进行操作。我已经设置了 Startup.cs 文件 public void ConfigureServices(I
我正在制作一个小型博客网站,但遇到了这个错误。有没有人可以帮忙?目前,我在 View 中定义的主页页面无法正常工作。我的 homepage.blade.php 页面,顺便说一句,在 front 文件夹
我刚开始使用基础,我使用脚手架创建了简单的 Posts 应用程序 我已经完成了以下步骤: rails new blog 然后将以下内容添加到 Gemfile gem 'foundation-rails
我有一个 HomeController,里面有很多 Actions。我希望用户无需输入 Home 即可访问我的操作。这是下面的路线 routes.MapRoute( name:
我正在尝试使用以下代码将查询结果从 HomeController 传递到主 Blade View ,但它似乎无法正常工作。 public function index() { $result=DB
我有一个 mvc 3 应用程序,其中我在 Index.cshtml View 上有一个输入表单。还有一个 webgrid,它有 edit,delete 按钮 根据这些操作链接,我需要更改我的提交按钮文
刚刚更新到 MVC 6 RC 1 并创建了一个新的 MVC 应用程序。 发现 Index action 方法被调用了两次。 Index()在 HomeControler : public string
首先,我最近想在JAVA EE上提高自己。而且还有一个题目,居然和我的问题一样。但是解决方案对我不起作用。这是该主题的链接: Exception encountered during context
我正在转换 Spring MVC app到 Spring Boot。Spring 应用程序有一个 Home Controller : @RestController public class Home
这是我使用 angularjs 的演示,用于创建服务文件并向 Controller 添加服务。 我的演示有两个问题: 一个是当我输入 之前 我得到这个错误, Error: [ng:areq] Argu
我正在查看 http://blog.backand.com/ng-grid-and-a-simple-rest-api/ 上的 ng-grid 教程 当我按照所有步骤操作时,它在控制台中显示以下错误
我正在从 ASP MVC Classic 迁移到 ASP Razor Pages。 只剩下一个 Controller 可以“迁移”:HomeController public class HomeC
我正在尝试为 Startup.cs 文件中的以下代码创建 DI。但是作为 .net 的新手,我很难找到解决方案。 var tokenAcquisition = context.HttpContext.
无法访问 homeController(模块 Controller )。单击“home”链接时,控制台出现错误“homeController 不是函数;未定义。 所以,我需要在哪里注册这个 Contr
我是一名优秀的程序员,十分优秀!