Why is bean not found during Spring Boot?

早过忘川 提交于 2021-01-29 08:18:04

问题


THIS PROBLEM IS SOLVED. SEE THE CHECKED ANSWER BELOW.

I reconfigured my DAOs to a more convenient way (by using JpaRepository) instead of doing all that boilerplate code manually. But now everytime I start the Spring Application it gives me the following error:

***************************

APPLICATION FAILED TO START

Description:

Field userRepository in DAO.UserDAOService required a bean of type 'DAO.UserRepository' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'DAO.UserRepository' in your configuration.

Process finished with exit code 1

********************************************************************

回答1:


SOLUTION: Just create sub-packages in the same package where you have your Spring appliciation located.

EXAMPLE OF SOLUTION CAN BE FOUND HERE: 'Field required a bean of type that could not be found.' error spring restful API using mongodb




回答2:


You've forgot to put an annotation on your repository class. That's why Spring cannot find that bean.

Try adding @Repository on top of your class definition.




回答3:


Add @Repository annotation then bean will created and autowired in service.

import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User , Integer>
{
}

And don't need to create bean in service

@Bean
public void setUserRepository(UserRepository userRepository)
{
    this.userRepository = userRepository;
}



回答4:


1) Make sure that you have your repository class in sub-package of ApplicationConfiguration class

2) Annotate the repository class with @Repositiry



来源:https://stackoverflow.com/questions/55064074/why-is-bean-not-found-during-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!