Spring boot bean扫描问题 @ComponentScan

[复制链接]
查看11 | 回复9 | 2021-1-27 05:42:41 | 显示全部楼层 |阅读模式
这几天一直被这个问题困扰,Springboot启动没报错,就是扫描不到需要注册的类。今天终于发现,Springboot的启动类需要放在需要扫描类的上层目录,这样才能扫描到同级或者子类需要注册的类。但是在网上我发现一个其他问题,有人说用@ComponentScan直接写需要扫描类的路径,但是我试验一直没成功。不知道为什么,麻烦帮忙看下!刚接触springboot先谢谢各位了。
下图是我要扫描的dao和service,但是启动后一直报错。报的错一看就是没扫描到类ReadingListRepository,这个类继承JpaRepository



2017-04-2522:45:57.349WARN7864---[main]ationConfigEmbeddedWebApplicationContext:Exceptionencounteredduringcontextinitialization-cancellingrefreshattempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'readingListController':Unsatisfieddependencyexpressedthroughfield'readingListRepository';nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:Noqualifyingbeanoftype'org.dawn.oket.dao.ReadingListRepository'available:expectedatleast1beanwhichqualifiesasautowirecandidate.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-04-2522:45:57.349INFO7864---[main]j.LocalContainerEntityManagerFactoryBean:ClosingJPAEntityManagerFactoryforpersistenceunit'default'
2017-04-2522:45:57.349INFO7864---[main]org.hibernate.tool.hbm2ddl.SchemaExport:HHH000227:Runninghbm2ddlschemaexport
2017-04-2522:45:57.349INFO7864---[main]org.hibernate.tool.hbm2ddl.SchemaExport:HHH000230:Schemaexportcomplete
2017-04-2522:45:57.359INFO7864---[main]o.apache.catalina.core.StandardService:StoppingserviceTomcat
2017-04-2522:45:57.399INFO7864---[main]utoConfigurationReportLoggingInitializer:
ErrorstartingApplicationContext.Todisplaytheauto-configurationreportre-runyourapplicationwith'debug'enabled.
2017-04-2522:45:57.681ERROR7864---[main]o.s.b.d.LoggingFailureAnalysisReporter:
***************************
APPLICATIONFAILEDTOSTART
***************************
Description:
FieldreadingListRepositoryinorg.dawn.oket.service.ReadingListControllerrequiredabeanoftype'org.dawn.oket.dao.ReadingListRepository'thatcouldnotbefound.
Action:
Considerdefiningabeanoftype'org.dawn.oket.dao.ReadingListRepository'inyourconfiguration.
DisconnectedfromthetargetVM,address:'127.0.0.1:52934',transport:'socket'
Processfinishedwithexitcode1




分 -->
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
这个注解扫描该类同级包下的类以及子包的类,如果有其他不同父子级的包,可以使用basePackage指定具体的包,推荐将启动类放在最上层的包中
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
请问楼主这个问题解决了吗?我也遇到这个问题了。弄了好几天了。网上说的文件夹位置,扫描类这些方法都试过了。还是不行
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
@EnableJpaRepositories
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
Repository类,加注解:@Repository
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
把你的启动类放在最上层的包中
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
ReadingListRepository这个写成普通的java类,不要用接口,然后在类上面加上@Service就可以了
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
application不要写在common里面,拿出来写
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
位置放错了,@ComponentScan放在@SpringBootApplication上面就行了
回复

使用道具 举报

千问 | 2021-1-27 05:42:41 | 显示全部楼层
没加注解没加注解
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行