SpringBoot bean无法注入的问题

暂时留个坑: 因为“Application类”的包名与service的包名不同,导致service下的类无法自动注入。

解决方案:

1 使用@ComponentScan 注解,如下文转载的文章,但是我自己改了还是有问题,暂时没找到原因,待补充;

2 将“Application类”与service放到同一个包下  -->可以正常运行


【转自】http://blog.csdn.net/u014695188/article/details/52263903

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.SpringBootJdbcDemoApplication.SpringBootJdbcDemoApplication': Unsatisfied dependency expressed through field 'userRepository': No qualifying bean of type [com.example.repositories.UserRepository] found for dependency [com.example.repositories.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repositories.UserRepository] found for dependency [com.example.repositories.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  
  2.     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)  
  3.     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)  

无法注入Dao中的Bean!

解决分析


后来经研究发现,SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! 
“Application类”是指SpringBoot项目入口类。这个类的位置很关键:

如果Application类所在的包为:io.github.gefangshuai.app,则只会扫描io.github.gefangshuai.app包及其所有子包,如果service或dao所在包不在io.github.gefangshuai.app及其子包下,则不会被扫描!


知道这一点非常关键,不知道spring文档里有没有给出说明,如果不知道还真是无从解决。

转:http://blog.csdn.NET/gefangshuai/article/details/50328451,http://412887952-qq-com.iteye.com/blog/2292733

在开发中我们知道Spring Boot默认会扫描启动类同包以及子包下的注解,那么如何进行改变这种扫描包的方式呢,原理很简单就是:
@ComponentScan注解进行指定要扫描的包以及要扫描的类。

接下来我们简单写个例子进行测试下。

第一步:新建两个新包
      我们在项目中新建两个包cn.kfit ; org.kfit;

第二步:新建两个测试类;
在这里为了方便测试,我们让我们的类在启动的时候就进行执行,所以我们就编写两个类,实现接口CommandLineRunner,这样在启动的时候我们就可以看到打印信息了。
cn.kfit.MyCommandLineRunner1  : 

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.kfit;  
  2.   
  3. import org.springframework.boot.CommandLineRunner;  
  4.   
  5. @Configuration  
  6. publicclass MyCommandLineRunner1 implements CommandLineRunner {  
  7.   
  8.     @Override  
  9.     publicvoid run(String... args) throws Exception {  
  10.        System.out.println("MyCommandLineRunner1.run()");  
  11.   
  12.     }  
  13. }  
org.kfit.MyCommandLineRunner2  : 
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package org.kfit;  
  2.   
  3. import org.springframework.boot.CommandLineRunner;  
  4.   
  5.   
  6. @Configuration  
  7. publicclass MyCommandLineRunner2 implements CommandLineRunner {  
  8.   
  9.     @Override  
  10.     publicvoid run(String... args) throws Exception {  
  11.   
  12.        System.out.println("MyCommandLineRunner2.run()");  
  13.   
  14.     }  
  15.   
  16. }  

第三步:启动类进行注解指定
在App.java类中加入如下注解:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //可以使用:basePackageClasses={},basePackages={}  
  2. @ComponentScan(basePackages={"cn.kfit","org.kfit"})  

 
启动如果看到打印信息:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. MyCommandLineRunner1.run()  
  2. MyCommandLineRunner2.run()  
说明我们配置成功了。

这时候你会发现,在App.java同包下的都没有被扫描了,所以如果也希望App.java包下的也同时被扫描的话,那么在进行指定包扫描的时候一定要进行指定配置:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @ComponentScan(basePackages={"cn.kfit","org.kfit","com.kfit"})  

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值