Reputation: 187
i did some search but i was unable to find out what's the problem. I'm aware the problem come from a ClassNotFoundException but i can't solve it.
error : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.it.service.applicationService com.it.controller.applicationController.applicationService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean
caused by : org.eclipse.jetty.servlet.ServletHolder$1: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.it.service.applicationService com.it.controller.applicationController.applicationService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean
my conf : version : 3.2.0.RELEASE
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
i autowired everything i need to (well, i think). here is my code :
DAO class:
package com.it.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.it.model.application;
@Repository("applicationDao")
public class applicationDaoImpl implements applicationDao {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<application> listeAll() {
return sessionFactory.getCurrentSession()
.createQuery("from application").list();
}
}
Service class :
package com.it.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.it.dao.applicationDao;
import com.it.model.application;
@Service("applicationService")
public class applicationServiceImpl implements applicationService {
@Autowired
private applicationDao applicationDao;
@Transactional
public List<application> listeAll() {
return applicationDao.listeAll();
}
}
Controller class :
package com.it.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.it.model.application;
import com.it.service.applicationService;
@Controller
@Configuration
@ComponentScan("com.it.service")
public class applicationController {
@Autowired
private applicationService applicationService;
@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {
map.put("application", new application());
map.put("applicationList", applicationService.listeAll());
return "application";
}
}
spring-servlet.xml :
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:packagesToScan="com.it"
p:dataSource-ref="dataSource"
/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
<!-- Translates Hibernate exceptions to Spring Data Access Exceptions -->
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
here i try several thing, localSessionFactoryBean, annotationfactorybean, hibernate 3 or 4 etc.. but none is working :(
also i have :
<context:component-scan base-package="com.it.controller" />
<context:component-scan base-package="com.it.dao" />
and services should be scanned by annotation but i tried to declare scan in xml as well.
If anyone can help me, it would be nice :) Thanks you in advance, Aure
Upvotes: 0
Views: 4659
Reputation: 1511
Where do you hand the hibernate.dialect to the sessionFactory? For a more complete view it would be helpful to see all parts of the configuration going into hibernate. So can you post your DataSource. But what I can say so far that the hibernate.dialect is a property that is mandatory to set. Besides the DataSource it is pretty much the only really essential configuration:
<property name="hibernate.dialect">org.hibernate.dialect.GenericDialect</property>
Try adding this to your sessionFactory Bean.
Upvotes: 0
Reputation: 94469
You need to add spring-orm
to your Maven Dependencies.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
Make sure your maven dependencies are being deployed, check out the first image in this answer: servlet packages not importing after converting project to maven project in eclipse
Upvotes: 1