当前位置: 首页 > 知识库问答 >
问题:

Spring+Hibernate:注入autowired依赖项失败

楚博雅
2023-03-14

我做了一些搜索,但我无法找出是什么问题。我知道这个问题来自于ClassNotFoundException,但我无法解决它。

<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";
}
}
<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"/>
<context:component-scan base-package="com.it.controller" />
<context:component-scan base-package="com.it.dao" />

共有1个答案

唐弘和
2023-03-14

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

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

确保部署了您的maven依赖项,查看答案中的第一个映像:在eclipse中将项目转换为maven项目后,servlet包不导入

 类似资料: