【发布时间】:2022-08-04 23:39:16
【问题描述】:
Spring Boot 2.3.12(由于我无法控制的原因,我无法更新到较新的版本)。
我已经用特定的扫描基础包定义了我的主要应用程序类,如下所示:
@SpringBootApplication(scanBasePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
}
)
@ComponentScan(
excludeFilters = {
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
}
)
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.run(args);
}
}
我想要完成的是两个都:
A)在应用程序的基本包之外包含一个包(因此org.otherpackage.ComponentScanMarker.class 引用在@SpringBootApplication 注释中)
和
B) 完全排除 HateoasConfiguration 类*.
我也试过这个:
@SpringBootApplication
@ComponentScan(
basePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
},
excludeFilters = {
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
}
)
这导致HateoasConfiguration 被加载,尽管excludeFilters。
我试过的另一个选择:
@SpringBootApplication(scanBasePackageClasses = {
MyApplication.class,
org.otherpackage.ComponentScanMarker.class
},
exclude = HateoasConfiguration.class
)
这会导致启动时出现异常并显示以下消息:
The following classes could not be excluded because they are not auto-configuration classes:
- org.springframework.hateoas.config.HateoasConfiguration
无论我尝试哪种注释属性组合,我都无法让它工作。尽管尝试将其排除,HateoasConfiguration 仍会被加载,或者 org.otherpackage 中的 @Components 会被加载不加载。我查看了几个不同的类似问题和答案,但没有一个包括对这两个目标的需求。
我怎样才能同时满足这两个需求,包括用于组件扫描的多个基础包,和排除类路径上的特定@Configuration 类?
*这个问题确实与 Spring HATEOAS 无关,它只是类路径上的 @Configuration 类的一个示例,但我希望 Spring Boot 忽略。以下是该类上的注释(源代码here):
@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {
-
请附上您尝试解决的特定问题。
* This question really has nothing to do with Spring HATEOAS, it\'s just an example of a third-party @ConfigurationSpring 有一些自动配置元素,例如 Hateoas。您身边的其他自定义组件应单独处理。请说清楚 -
@PanagiotisBougioukos 我不明白你的评论。该问题以多种方式说明了要完成的目标是什么。例如,\“我想要完成的是两者:...\”就在那里。
-
请检查您的问题。你指定了一个特定的问题,然后你提到了
* This question really has nothing to do with Spring HATEOAS, it\'s just an example of a third-party @Configuration class that is on the classpath but I want Spring Boot to ignore.。 Spring Hateoas 不是一个简单的 3rd 方库,而是一个 spring 库,开发人员可用的操作与一些自定义的外部 3rd 方库不同 -
我从句子中删除了“第三方”。
-
其他一些配置或自动配置是否可能依赖于这个类?在这种情况下,您还必须禁用它们。
标签: spring spring-boot component-scan