How to scan multiple paths using the @ComponentScan annotation?(如何使用@ComponentScan 注解扫描多个路径?)
问题描述
我正在使用 Spring 3.1 并使用 @Configuration 和 @ComponentScan 属性引导应用程序.
I'm using Spring 3.1 and bootstrapping an application using the @Configuration and @ComponentScan attributes.
真正的开始是用
new AnnotationConfigApplicationContext(MyRootConfigurationClass.class);
这个配置类被注释了
@Configuration
@ComponentScan("com.my.package")
public class MyRootConfigurationClass
这很好用.但是我想更具体地了解我扫描的包,所以我尝试了.
and this works fine. However I'd like to be more specific about the packages I scan so I tried.
@Configuration
@ComponentScan("com.my.package.first,com.my.package.second")
public class MyRootConfigurationClass
但是这失败了,错误提示我找不到使用 @Component 注释指定的组件.
However this fails with errors telling me it can't find components specified using the @Component annotation.
我所追求的正确方法是什么?
What is the correct way to do what I'm after?
谢谢
推荐答案
@ComponentScan 使用字符串数组,像这样:
@ComponentScan uses string array, like this:
@ComponentScan({"com.my.package.first","com.my.package.second"})
当你只在一个字符串中提供多个包名时,Spring会将其解释为一个包名,因此无法找到它.
When you provide multiple package names in only one string, Spring interprets this as one package name, and thus can't find it.
这篇关于如何使用@ComponentScan 注解扫描多个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用@ComponentScan 注解扫描多个路径?
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- JPA惰性列表上的流 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
