开发者社区> 问答> 正文

Spring整合Hibernate过程中一个Service只能处理一个跟数据库连接的dao吗?报错

具体情况:使用@Service @Transactional处理跟数据库连接的dao时,通过@Autowired自动引入的dao只能有一个吗?我已实现了对TeacherDao的自动引入并可以完成相关增删改操作,可是当我想用同一个labservice再次引用AnnouncementDao去连接相应的数据库表进行查询操作时发现报错:

Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.service.LabService a.controller.HomeController.labService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'labServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.dao.AnnouncementDao a.service.LabServiceImpl.announcementDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [a.dao.AnnouncementDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Spring-servlet.xml配置文件相关内容:

    <!-- 指定Hibernate配置文件的位置-->
        <property name="configLocations">  
            <list>  
                <value>  
                    classpath*:hibernate.cfg.xml  
                </value>  
            </list>  
        </property> 


    </bean>


    <!-- 配置Spring事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    <!-- 配置Spring事务管理-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

hibernate.cfg.xml  :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC  
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 


<hibernate-configuration>
    <session-factory>
        <mapping class="a.model.Manager" />
        <mapping class="a.model.Teacher" />
        <mapping class="a.model.Message" />
        <mapping class="a.model.Announcement" />
      
    </session-factory>          
</hibernate-configuration>

a.model.Manager:实体类,映射数据库属性。

package a.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Manager")
public class Manager {

@Id
@Column(name="mno")
private String mno;
@Column(name="mname")
private String mname;
@Column(name="mpassword")
private String mpassword;
@Column(name="mgender")
private String mgender;
@Column(name="mbirthday")
//@DateTimeFormat(pattern="yyy-mm-dd")
private String mbirthday;
@Column(name="mtel")
private String mtel;
@Column(name="mschool")
private String mschool;
....set(),get()


a.service.labservice:

package a.service;
import a.dao.AnnouncementDao;
import a.dao.ManagerDao;
import a.dao.MessageDao;
import a.dao.TeacherDao;
import a.model.Teacher;
import a.model.Announcement;
import a.model.Message;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class LabServiceImpl implements LabService {


@Autowired(required=false)
private TeacherDao teacherDao;
public Teacher getTeacherDao(String no) {
return teacherDao.getTeacher(no);
}
public void setTeacherDao( TeacherDao teacherDao) {
this.teacherDao = teacherDao;
}

@Autowired
private ManagerDao managerDao;

public ManagerDao getManagerDao() {
return managerDao;
}
public void setManagerDao(ManagerDao managerDao) {
this.managerDao = managerDao;
}


@Autowired(required=false)
private MessageDao messageDao;
@Autowired
private AnnouncementDao announcementDao;



展开
收起
爱吃鱼的程序员 2020-06-09 14:59:05 734 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

     Noqualifyingbeanoftype[a.dao.AnnouncementDao]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

    atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)

    自己顶一下,报错!!

    2020-06-09 14:59:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载