spring boot启动不扫描创建bean怎么回事?
说明你的spring boot启动时的application类不在io.github.gefangshuai.app及其子包下。
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描的。“Application类”是指SpringBoot项目入口类。如果Application类所在的包为:io.github.gefangshuai.app,则只会扫描io.github.gefangshuai.app包及其所有子包,如果service或dao所在包不在io.github.gefangshuai.app及其子包下,则不会被扫描。
改变这种扫描包的方式的原理很简单:用@ComponentScan注解进行指定要扫描的包以及要扫描的类。
可以用以下方式测试:
第一步:新建两个包cn.kfit ; org.kfit;
第二步:新建两个测试类;
在这里为了方便测试,我们让我们的类在启动的时候就进行执行,所以就编写两个类,实现接口CommandLineRunner,这样在启动的时候我们就可以看到打印信息了。
cn.kfit.MyCommandLineRunner1 :
package cn.kfit;
import org.springframework.boot.CommandLineRunner;
@Configuration
publicclass MyCommandLineRunner1 implements CommandLineRunner {
@Override
publicvoid run(String... args) throws Exception {
System.out.println("MyCommandLineRunner1.run()");
}
}
org.kfit.MyCommandLineRunner2 :
package org.kfit;
import org.springframework.boot.CommandLineRunner;
@Configuration
publicclass MyCommandLineRunner2 implements CommandLineRunner {
@Override
publicvoid run(String... args) throws Exception {
System.out.println("MyCommandLineRunner2.run()");
}
}
第三步:启动类进行注解指定;
在App.java类中加入如下注解:
//可以使用:basePackageClasses={},basePackages={}
@ComponentScan(basePackages={"cn.kfit","org.kfit"})
启动时如果看到打印信息:
则说明配置成功。
1.这几天一直被这个问题困扰,Spring boot启动没报错,就是扫描不到需要注册的类。今天终于发现,Spring boot的启动类需要放在需要扫描类的上层目录,这样才能扫描到同级或者子类需要注册的类。 但是在网上我发现一个其他问题,有人说用 @ComponentScan直接写需要扫描类的路径,但是我试验一直没成功。不知道为什么,麻烦帮忙看下! 刚接触spring boot 先谢谢各位了 。
2.下图是我要扫描的dao和service,但是启动后一直报错。报的错一看就是没扫描到类ReadingListRepository,这个类继承JpaRepository
3.2017-04-25 22:45:57.349 WARN 7864 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'readingListController': Unsatisfied dependency expressed through field 'readingListRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.dawn.oket.dao.ReadingListRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-04-25 22:45:57.349 INFO 7864 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-04-25 22:45:57.349 INFO 7864 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-04-25 22:45:57.349 INFO 7864 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-04-25 22:45:57.359 INFO 7864 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-04-25 22:45:57.399 INFO 7864 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-04-25 22:45:57.681 ERROR 7864 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field readingListRepository in org.dawn.oket.service.ReadingListController required a bean of type 'org.dawn.oket.dao.ReadingListRepository' that could not be found.
Action:
Consider defining a bean of type 'org.dawn.oket.dao.ReadingListRepository' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:52934', transport: 'socket'
Process finished with exit code 1
参考资料
csdn.csdn[引用时间2017-12-27]