0

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

错误:org.springframework.beans.factory.BeanCreationException:创建名为“applicationController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 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:无法自动装配字段:私有 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

我的配置:版本: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";
}
}

弹簧-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 中声明扫描。

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

4

2 回答 2

1

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

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

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

于 2013-05-07T09:41:10.037 回答
0

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

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

尝试将此添加到您的 sessionFactory Bean。

于 2013-05-07T11:09:21.673 回答