@componentscan用于项目中的每个包

kzipqqlq  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(264)

有没有可能让 @ComponentScan 整个 Spring 启动项目的工作?现在,当我创建一个新包时,我需要将它添加到 @ComponenScan 否则将找不到bean。如果允许在项目中搜索任何地方,是否会导致性能问题或任何其他问题?

Example:
first component StorageRoom\backend\src\main\java\com.storeage (here is main class with @ComponentScan annotation)
second component StorageRoom\backend\src\main\java\security
third component StorageRoom\backend\src\main\java\component3
olqngx59

olqngx591#

@ComponentScan 如果没有参数,则通知spring扫描当前包及其所有子包。因此,对于您的问题,我建议您将主类放在应用程序的根目录下,然后用 @ComponentScan 在那里。就像这里- StorageRoom\backend\src\main\java\SpringBootApplicationMain.java 更多信息请访问:https://www.baeldung.com/spring-component-scanning

相关问题