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

桔子大人 发布于 2016/05/01 10:44
阅读 503
收藏 0

具体情况:使用@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;



加载中
0
桔子大人
桔子大人
 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)}

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

自己顶一下,报错!!

返回顶部
顶部