首页 > 解决方案 > Parameter 0 of constructor in ServiceImpl required a bean of type DAO that could not be found

问题描述

While creating a service that will save the object in the DB using Hibernate as ORM, I'm unable to start the application.

I'm using Spring Boot and Hibernate. My ServiceImpl:

@Service
public class PropertyServiceImpl implements PropertyService{

private PropertyDAO propertyDAO;

    public PropertyServiceImpl(){
    System.out.println("inside propertyserviceimpl constructor");
}

@Autowired
public PropertyServiceImpl(PropertyDAO propertyDAO){
    this.propertyDAO = propertyDAO;
    System.out.println("inside save");
}

@Transactional
public void save(Property property) {
    propertyDAO.save(property);
}

@Override
public List findAll() {
    // TODO Auto-generated method stub
    return null;
}

}

PropertyDAO.java

public interface PropertyDAO {

public void save(Property property);

 }

PropertyDAOImpl implement the DAO

public class PropertyDAOImpl implements PropertyDAO{

@Autowired
private SessionFactory sessionFactory;

public void save(Property property) {
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.saveOrUpdate(property);
}

}

I'm getting the following error message when I start the SpringBoot Application.

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.flarow.flarowhomes.services.PropertyServiceImpl required a bean of type 'com.flarow.flarowhomes.dao.PropertyDAO' that could not be found.


Action:

Consider defining a bean of type 'com.flarow.flarowhomes.dao.PropertyDAO' in your configuration.

标签: javaspringhibernatespring-boot

解决方案


@Repository添加到您的 DAO 实现类中,以便找到它:

@Repository
public class PropertyDAOImpl implements PropertyDAO {

实现传统 Java EE 模式(如“数据访问对象”)的团队也可以将此原型应用于 DAO 类,但在这样做之前应注意了解数据访问对象和 DDD 样式存储库之间的区别。


推荐阅读