问题
项目中使用了@ComponentScan
注解,但是idea中 basePackages
指定的包路径一直报红,看了下basePackages是个数组,于是乎好奇起来,basePackages = {}
是填String数组还是填用逗号隔开的String呢?
@ComponentScan(basePackages = {"com.aaa.aaa,com.bbb.bbb"}
放码过来
Spring 启动执行代码
图片.pngorg.springframework.context.annotation.ComponentScanAnnotationParser#parse
Set<String> basePackages = new LinkedHashSet<>();
String[] basePackagesArray = componentScan.getStringArray("basePackages");
for (String pkg : basePackagesArray) {
String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
Collections.addAll(basePackages, tokenized);
}
强行翻译:
在单个String字符串中,多个上下文配置路径之间的 这些字符",; \t\n"
中的任意一个 都会被认为是分隔符
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS
/**
* Any number of these characters are considered delimiters between
* multiple context config paths in a single String value.
* @see org.springframework.context.support.AbstractXmlApplicationContext#setConfigLocation
* @see org.springframework.web.context.ContextLoader#CONFIG_LOCATION_PARAM
* @see org.springframework.web.servlet.FrameworkServlet#setContextConfigLocation
*/
String CONFIG_LOCATION_DELIMITERS = ",; \t\n";
结论
@ComponentScan(basePackages = {})填String数组 或者 用逗号隔开的String 都可以;为了idea显示兼容一点,我还是该成了 String数组
网友评论