Java ServiceImpl中构造函数的参数0需要找不到DAO类型的bean
在使用Hibernate作为ORM创建将对象保存在DB中的服务时,我无法启动应用程序Java ServiceImpl中构造函数的参数0需要找不到DAO类型的bean,java,spring,hibernate,spring-boot,Java,Spring,Hibernate,Spring Boot,在使用Hibernate作为ORM创建将对象保存在DB中的服务时,我无法启动应用程序 *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.flarow.flarowhomes.services.PropertyServiceImpl required a bean of type 'c
***************************
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.
我正在使用SpringBoot和Hibernate。我的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实现DAO
public class PropertyDAOImpl implements PropertyDAO{
@Autowired
private SessionFactory sessionFactory;
public void save(Property property) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(property);
}
}
我在启动SpringBoot应用程序时收到以下错误消息
***************************
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.
按如下方式更改您的PropertyDAO:
public interface PropertyDAO extends JpaRepository<Property, Integer>{ }
public interface PropertyDAO扩展了JpaRepository{}
添加到DAO实现类中,以便找到:
@Repository
public class PropertyDAOImpl implements PropertyDAO {
实现“数据访问对象”等传统JavaEE模式的团队也可以将此原型应用于DAO类,不过在这样做之前,应该注意理解数据访问对象和DDD样式存储库之间的区别
显示未找到的
PropertyDAO
类使用PropertyDAO更新了问题,但标记为Spring bean并实现该PropertyDAO接口的具体类在哪里?PropertyDAOImpl实现PropertyDAO但它是Spring bean吗?它是否有一个组件注释(或服务注释,或存储库注释),将其标记为Springbean?Spring只注入Sring bean。不是随机类。这需要我从void更改save方法的返回类型。是的,您将返回您保存的属性。这有助于我消除所面临的错误。但是引入了一个新的错误Description:com.flarow.flarowhomes.dao.PropertyDAOImpl中的字段sessionFactory需要一个找不到的类型为“org.hibernate.sessionFactory”的bean。操作:考虑在配置中定义一个类型为“org .Hibernate .SeaStudio”的bean。