首先,我最近试图在JAVA EE中提高自己。还有另一个话题,实际上与我的问题相同。但解决方案对我不起作用。以下是该主题的链接:
另一个话题是: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
无论如何,我会提供我所面临的错误,
错误讯息:
Org.springframework.beans.factory.UnsatisfiedDependencyException:错误>创建名称为' homeController':不满意的依赖关系表达>通过字段' 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>
我尝试过的事情:
bean id =&#34; productDao&#34;类=&#34; com.emusicstore.dao.impl.ProductDaoImpl&#34; /&GT;
使用@Component
将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
答案 0 :(得分:0)
尝试添加
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>4.0.1.Final</version>
</dependency>
给你pom.xml
档案