Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ContainerEnumScan 注解无法支持带有通配符的路径 #267

Closed
Createsequence opened this issue Apr 24, 2024 · 1 comment
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Createsequence
Copy link
Collaborator

@ContainerEnumScan 注解无法支持带有通配符的路径,现在有如下路径:

  • com.example.core.enums
  • com.example.api.enums

则通过 com.example.**com.example.**.enumscom.example.**.*.enums 均无法扫描到对应的枚举。

@ContainerConstantScan@OperatorScan 似乎也是这个情况。

@Createsequence Createsequence added the bug Something isn't working label Apr 24, 2024
@Createsequence Createsequence added this to the release 2.8.0 milestone Apr 24, 2024
Createsequence added a commit that referenced this issue Apr 30, 2024
…ectory, components in some directories will not be loaded (GitHub #267)
Createsequence added a commit that referenced this issue Apr 30, 2024
…ectory, components in some directories will not be loaded (GitHub #267)
Createsequence added a commit that referenced this issue Apr 30, 2024
…ectory, components in some directories will not be loaded (GitHub #267)
@Createsequence
Copy link
Collaborator Author

Createsequence commented Apr 30, 2024

这个问题似乎是底层使用的 PathMatchingResourcePatternResolver 造成的,参见这一段注释:

WARNING: Ant-style patterns with "classpath:" resources are not guaranteed to find matching resources if the root package to search is available in multiple class path locations. This is because a resource such as

  com/mycompany/package1/service-context.xml

may be in only one location, but when a path such as

  classpath:com/mycompany/**/service-context.xml

is used to try to resolve it, the resolver will work off the (first) URL returned by getResource("com/mycompany");. If this base package node exists in multiple classloader locations, the actual end resource may not be underneath. Therefore, preferably, use "classpath*:" with the same Ant-style pattern in such a case, which will search all class path locations that contain the root package.

从字面上理解,如果我们使用 classpath:com/example/package 或者 com/example/package —— 默认情况下两者是等价的 —— 去从项目的根路径开始扫描时,如果引入了多个依赖中存在相同的包路径,那么仅会加载首个匹配的路径下的类。

举个例子,假如项目中引入了 A 和 B 两个依赖,它们的目录结构分别是 com/example/package/acom/example/package/b,此时,由于我们指定扫描的路径是 com/example/package,当匹配到 com/example/package/a 时就会认为已经找到了,后面就不会再扫描 com/example/package/b 路径了。

这个问题通过在扫描路径前端添加 classpath*/ 即可解决。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant