具体情况:使用@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;
}
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)
自己顶一下,报错!!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。