First of all, I am recently trying to improve myself in JAVA EE. And there is another topic, actually the same problem as mine. But the solution did not work for me. Here is the link of that topic:
Also another topic is: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
Anyways I will provide the errors I am facing with,
Error Message:
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
Here is 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>
The things that I have tried:
- register bean object in applicationContext.xml
bean id="productDao" class="com.emusicstore.dao.impl.ProductDaoImpl" />
Annotating ProductDao class with @Component
Adding Hibernate Core jar file to Project Structure
Here is also the pic of my Project Structure: Project Structure
Full error stack:
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
<context:component-scan base-package="com.emusicstore" />to your` applicationContext.xml` and try?