spring + hibernate:自动连接依赖项的注入失败

时间:2013-05-07 09:39:18

标签: spring hibernate autowired

我做了一些搜索,但我无法找出问题所在。 我知道问题来自ClassNotFoundException但我无法解决它。

错误: org.springframework.beans.factory.BeanCreationException:创建名为'applicationController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.it.service.applicationService com.it.controller.applicationController.applicationService;嵌套异常是org.springframework.beans.factory.CannotLoadBeanClassException:无法为ServletContext资源[/WEB-INF/spring-servlet.xml]中定义的名称为'sessionFactory'的bean找到类[org.springframework.orm.hibernate4.LocalSessionFactoryBean] ;嵌套异常是java.lang.ClassNotFoundException:org.springframework.orm.hibernate4.LocalSessionFactoryBean

引起的: org.eclipse.jetty.servlet.ServletHolder $ 1:org.springframework.beans.factory.BeanCreationException:创建名为'applicationController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.it.service.applicationService com.it.controller.applicationController.applicationService;嵌套异常是org.springframework.beans.factory.CannotLoadBeanClassException:无法为ServletContext资源[/WEB-INF/spring-servlet.xml]中定义的名称为'sessionFactory'的bean找到类[org.springframework.orm.hibernate4.LocalSessionFactoryBean] ;嵌套异常是java.lang.ClassNotFoundException:org.springframework.orm.hibernate4.LocalSessionFactoryBean

我的conf:版本: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>

我自动装配了我需要的一切(好吧,我认为)。这是我的代码:

DAO课程:

    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();
}

    }

服务类:

    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();
}

    }

控制器类:

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"/>

这里我尝试了几件事,localSessionFactoryBean,annotationfactorybean,hibernate 3或4等。但是没有一个工作:(

我也有:

<context:component-scan base-package="com.it.controller" />
<context:component-scan base-package="com.it.dao" />

并且应该通过注释扫描服务,但我也尝试在xml中声明扫描。

如果有人可以帮助我,那就太好了:) 提前谢谢你, 欧尔

2 个答案:

答案 0 :(得分:1)

您需要将spring-orm添加到您的Maven依赖项。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>

确保部署了maven依赖项,请查看此答案中的第一张图片:servlet packages not importing after converting project to maven project in eclipse

答案 1 :(得分:0)

你在哪里将hibernate.dialect交给sessionFactory?要获得更完整的视图,查看配置的所有部分进入休眠状态会很有帮助。那么你可以发布你的数据源。   但到目前为止,我可以说hibernate.dialect是一个必须设置的属性。除了DataSource,它几乎是唯一真正必要的配置:

<property name="hibernate.dialect">org.hibernate.dialect.GenericDialect</property>

尝试将此添加到sessionFactory Bean。