SpringBoot学习——@Autowired自动注入报:could not be found问题的理解和解决方案
学习SpringBoot时,学到需要在Controller中注入类userRepository
,使用这个类我们获取用户信息
@Autowired
private UserRepository userRepository;
但是再启动springboot时,控制台报错:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.coldcrack.spring.boot.blog.thymeleafinit.controller.UserController required a bean of type 'com.coldcrack.spring.boot.blog.thymeleafinit.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.coldcrack.spring.boot.blog.thymeleafinit.repository.UserRepository' in your configuration.
描述是没有找到这个包,我们在对应的包的类中添加注解就可以了,比如这个包我用@Repository
注解即可。