sofastack / sofa-ark

SOFAArk is a light-weight,java based classloader isolation framework.
https://www.sofastack.tech/projects/sofa-boot/sofa-ark-readme/
Apache License 2.0
1.56k stars 497 forks source link

【bug】静态合并部署时,宿主应用会把子业务应用中的Bean也会扫描注册到自己的ApplicationContext中,而不是仅仅把子应用作为一个特殊Jar包 #612

Closed zhengchangqing closed 1 year ago

zhengchangqing commented 1 year ago

Describe the bug

静态合并部署时,为什么宿主应用会把子业务应用中的Bean也会扫描注册到自己的ApplicationContext中,而不是仅仅把子应用作为一个特殊Jar包?

Steps to reproduce

宿主应用: sofa-ark-spring-guides 业务应用: spring-boot-ark-biz

① 宿主应用修改启动类:

@SpringBootApplication
@ComponentScan({"com.alipay.sofa"})

② 宿主应用修改application.properties:

logging.level.org.springframework.context=DEBUG
logging.level.org.springframework.web=DEBUG

③ 业务应用修改SampleController:

@RestController("/sample")
public class SampleController {
}

④ 启动宿主应用,观察启动日志:

23-02-17 19:00:49.298 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\WorkSpace\sofa_workspace\sofa-ark-spring-guides\target\classes\com\alipay\sofa\impl\SampleServiceImpl.class]
23-02-17 19:00:49.312 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/D:/repository/com/alipay/sofa/spring-boot-ark-biz/0.0.1-SNAPSHOT/spring-boot-ark-biz-0.0.1-SNAPSHOT-ark-biz.jar!/com/alipay/sofa/springbootarkbiz/rest/SampleController.class]
23-02-17 19:00:49.313 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/D:/repository/com/alipay/sofa/spring-boot-ark-biz/0.0.1-SNAPSHOT/spring-boot-ark-biz-0.0.1-SNAPSHOT-ark-biz.jar!/com/alipay/sofa/springbootarkbiz/SpringBootArkBizApplication.class]
23-02-17 19:00:49.341 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/D:/repository/com/alipay/sofa/sofa-ark-springboot-starter/2.1.1/sofa-ark-springboot-starter-2.1.1.jar!/com/alipay/sofa/ark/springboot/ArkAutoConfiguration$EmbeddedArkTomcat.class]
23-02-17 19:00:49.347 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/D:/repository/com/alipay/sofa/sofa-ark-springboot-starter/2.1.1/sofa-ark-springboot-starter-2.1.1.jar!/com/alipay/sofa/ark/springboot/ArkAutoConfiguration.class]
23-02-17 19:00:49.365 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/D:/repository/com/alipay/sofa/sofa-ark-compatible-springboot2/2.1.1/sofa-ark-compatible-springboot2-2.1.1.jar!/com/alipay/sofa/ark/springboot2/CompatibleSpringBoot2AutoConfiguration.class]
23-02-17 19:00:49.459 [main] DEBUG o.s.c.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/D:/repository/com/alipay/sofa/spring-boot-ark-biz/0.0.1-SNAPSHOT/spring-boot-ark-biz-0.0.1-SNAPSHOT-ark-biz.jar!/com/alipay/sofa/springbootarkbiz/rest/SampleController.class]

宿主应用启动时,便可以扫描注册/sample路径。

Environment

penglinzhang commented 1 year ago

结论:这个可能不是bug 根因:由于你的宿主应用@ComponentScan扫描了 “com.alipay.sofa”,并且你的业务bean也是com/alipay/sofa路径下的 解决:你可以尝试将业务包spring-boot-ark-biz的路径改成自定义的

lylingzhen commented 1 year ago

我们确实发现了这个问题,正在修复中。

lylingzhen commented 1 year ago

计划 4 月上旬 Release 版本修复。

penglinzhang commented 1 year ago

计划 4 月上旬 Release 版本修复。

有一个疑问,上面能扫描的原因是@ComponentScan指定了普通ark-biz包的路径才导致,非master的也能扫描进来,这也算是bug吗

yuanyuancin commented 1 year ago

有一个疑问,上面能扫描的原因是@componentscan指定了普通ark-biz包的路径才导致,非master的也能扫描进来,这也算是bug吗

@penglinzhang

Ark 这边认为这不能算是bug,是by design的

  1. 扫描范围配置过大以至于扫描到模块bean,并非是 静态合并部署 特有的问题,如普通 spring-boot 应用同理,
  2. 基座正是在 classpath 中扫描到 “我们认可的” 模块的,classpath 无法隔离
  3. 将会在静态合并部署 demo 中对这一点做一个说明
yuanyuancin commented 1 year ago

@FlyAbner 可以把基座和模块的componentScan隔离开,比如基座配置 com.alipay.sofa.base,模块配置 com.alipay.sofa.module;

如若使用了 @ImportResource,同理。

lylingzhen commented 1 year ago

ByDesign,先 Close。