新手请教:spring boot 中@Autowired注解无法自动注入的错误。

huyuleizj 2016-07-01 11:26:05
学习spring boot,遇到@Autowired总是报错,自己折腾了半天也没搞定,请大家帮忙看看:
这是controller和dao的调用和目录结构


这是一个简单的dao,参照书上写的


结果启动时报错BeanCreationException:

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userController':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.caizhaotu.dao.user.UserRepository com.caizhaotu.controller.user.UserController.userRepository;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException

好像是spring注入bean的时候错误,但不知道这个springboot改怎么配置,因为我看得书上都说是自动配置的啊?
...全文
44281 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
david_van 2019-09-03
  • 打赏
  • 举报
回复
List<User> findByUserName(String userName); jpa需要一定的规范, Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException 异常信息里面说是没有这样的元素异常,你这边查询需要应该userName参数
亦寒2017 2019-01-10
  • 打赏
  • 举报
回复
这个方法更简单,只需要加个static,而且说明本质问题了。https://blog.csdn.net/m0_37202351/article/details/86255132
DariusEle 2018-11-13
  • 打赏
  • 举报
回复
(required=false)
awastocean 2018-01-29
  • 打赏
  • 举报
回复
applicationw位置正确,然后也报这错,另个项目一样的位置没报错。目前确认jar包里有,但是注解引入失败。
melodykke 2017-12-25
  • 打赏
  • 举报
回复
引用 5 楼 u012097096 的回复:
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类。这个类的位置很关键: 如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描! 即, 把Application类放到dao、service所在包的上级,com.boot.Application 知道这一点非常关键,不知道spring文档里有没有给出说明,如果不知道还真是无从解决
—————————————————————————————————————————————————————————— 五楼提到的确实是很多人没注意就容易掉坑的地方
阿衰弟 2017-11-29
  • 打赏
  • 举报
回复
在启动类上加 @ServletComponentScan
深夜猿 2017-11-13
  • 打赏
  • 举报
回复
不行啊 在action里面加了@EnableAutoConfiguration 还是一样
丿灬林林 2017-11-02
  • 打赏
  • 举报
回复
我的也不对,楼主解决了给个建议
抱枕抱枕 2017-08-27
  • 打赏
  • 举报
回复
楼主解决了吗?我也是这样的楼上的注解我都加了,包的路径也是对的。可是还是不对
monitor-jone 2017-07-08
  • 打赏
  • 举报
回复
5楼厉害了。
阿惯 2017-07-06
  • 打赏
  • 举报
回复
引用 14 楼 chinrui 的回复:
[quote=引用 13 楼 fernando_2008 的回复:] 我的启动类的包命是这个 controller的包命如下 启动的时候还是会报这个错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tempControllor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.templates.dao.UserDao com.templates.controller.TempControllor.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.templates.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 求解~~~~
@SpringBootApplication 注解效果等同于 @Configuration,@EnableAutoConfiguration 及 @ComponentScan 这三个注解一起使用,所以不要在 Controller 上面添加 @EnableAutoConfiguration [/quote] 启动类上加上这个 @EnableJpaRepositories
qchery 2017-05-01
  • 打赏
  • 举报
回复
引用 13 楼 fernando_2008 的回复:
我的启动类的包命是这个 controller的包命如下 启动的时候还是会报这个错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tempControllor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.templates.dao.UserDao com.templates.controller.TempControllor.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.templates.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 求解~~~~
@SpringBootApplication 注解效果等同于 @Configuration,@EnableAutoConfiguration 及 @ComponentScan 这三个注解一起使用,所以不要在 Controller 上面添加 @EnableAutoConfiguration
fernando_2008 2017-04-30
  • 打赏
  • 举报
回复
我的启动类的包命是这个


controller的包命如下

启动的时候还是会报这个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tempControllor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.templates.dao.UserDao com.templates.controller.TempControllor.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.templates.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

求解~~~~
花开浪漫拾 2017-03-24
  • 打赏
  • 举报
回复
引用 5 楼 u012097096 的回复:
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类。这个类的位置很关键: 如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描! 即, 把Application类放到dao、service所在包的上级,com.boot.Application 知道这一点非常关键,不知道spring文档里有没有给出说明,如果不知道还真是无从解决
五楼正解, application.java 可以放在controller、service、dao层都可以,但是要保证application.java 包位置处于所有层的上级 ,比如com.xxx.web、com.xxx.service、com.xxx.dao。把application.java放在com.xxx即可
baidu_34821780 2017-03-15
  • 打赏
  • 举报
回复
引用 1 楼 qq_15915835 的回复:
dao没有实现类,也没有注解,咋注入啊
没玩过就不要乱吹b,
baidu_34821780 2017-03-15
  • 打赏
  • 举报
回复
引用 2 楼 qnmdcsdn 的回复:
+1
jpa需要注解?需要实现?先好好学习技术吧。
丢雷老某 2017-03-13
  • 打赏
  • 举报
回复
五楼 屌 ~~~
搬砖阶段 2017-02-09
  • 打赏
  • 举报
回复
555 还是五楼厉害
雨冰 2017-01-12
  • 打赏
  • 举报
回复
5楼正解啊,在仔细看一下项目的结构,application必须在所扫描的包的第一层。再就是继承了JPA 是不需要再写实现类的。你的代码应该没错
malachi95 2016-11-18
  • 打赏
  • 举报
回复
楼主解决了这个问题吗,我也是一样,他们说的包的路径我也是对的,实在不知道怎么解决了
加载更多回复(5)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧