jpa Why is bean not found during Spring Boot?

np8igboo  于 2022-11-14  发布在  Spring
关注(0)|答案(5)|浏览(182)

我用一种更方便的方式重新配置了我的DAO(通过使用JpaRepository),而不是手动执行所有的样板代码。

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
qhhrdooz

qhhrdooz1#

解决方案:只需在Spring应用程序所在的包中创建子包。
解决方案示例可在此处找到:'Field required a bean of type that could not be found.' error spring restful API using mongodb

kr98yfug

kr98yfug2#

您忘记在存储库类上添加注解,这就是Spring找不到该bean的原因。
尝试在类定义的顶部添加@Repository

7fyelxc5

7fyelxc53#

添加@Repository注解,然后将创建Bean并在服务中自动连接。

import org.springframework.stereotype.Repository;

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

并且不需要在服务中创建bean

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

cx6n0qe34#

1.确保您的repository类位于ApplicationConfiguration类的子包中。
1.使用@Repository注解储存库类别。

q9yhzks0

q9yhzks05#

除了前面的答案之外,IDE还经常会提示您错误地导入Bean类的注解,例如,对于@Service注解Bean,请确保导入:

import org.springframework.stereotype.Service;

而不是像这样:

import org.jvnet.hk2.annotations.Service

相关问题